Exemplo n.º 1
0
        public async void AddPostOk()
        {
            // Arrange
            List <WsClient>    expectedWsClients = _fixture.CreateMany <WsClient>(1).ToList();
            PostDto            postDto           = _fixture.Create <PostDto>();
            IdResultDto <long> expectedResult    = _fixture.Build <IdResultDto <long> >()
                                                   .With(x => x.IsOk, true)
                                                   .Create();

            _postServiceMock.Setup(x => x.AddRequestAsync(postDto, It.IsAny <WsClient>())).ReturnsAsync(expectedResult);
            _cacheServiceMock.Setup(x => x.GetWsClientsAsync()).ReturnsAsync(expectedWsClients);

            // Act
            IActionResult actionResult = await _controller.AddPost(postDto);

            // Assert
            actionResult.Should().NotBeNull().And.BeOfType <OkObjectResult>();

            _cacheServiceMock.Verify(x => x.GetWsClientsAsync(), Times.Once);
            _postServiceMock.Verify(x => x.AddRequestAsync(postDto, It.IsAny <WsClient>()), Times.Once);
            _loggerMock.Verify(m => m.Log(
                                   LogLevel.Debug,
                                   0,
                                   It.Is <FormattedLogValues>(v => v.ToString()
                                                              .Contains($"Ok for: '{postDto.GetType().Name}' -> Id: '{expectedResult.Id}'")),
                                   null,
                                   It.IsAny <Func <object, Exception, string> >()
                                   ));

            var objectResult = actionResult as OkObjectResult;

            objectResult.StatusCode.Should().Be(StatusCodes.Status200OK);
            objectResult.Value.Should().NotBeNull().And.BeOfType(typeof(IdResultDto <long>));
            var idResultDto = objectResult.Value as IdResultDto <long>;

            idResultDto.IsOk.Should().Be(true);
            idResultDto.Message.Should().NotBeNull().And.Be(expectedResult.Message);
            idResultDto.Id.Should().Be(expectedResult.Id);
        }
Exemplo n.º 2
0
        public async void AddPostNotOk()
        {
            // Arrange
            List <WsClient>    expectedWsClients = _fixture.CreateMany <WsClient>(1).ToList();
            PostDto            postDto           = _fixture.Create <PostDto>();
            IdResultDto <long> expectedResult    = _fixture.Build <IdResultDto <long> >()
                                                   .With(x => x.IsOk, false).With(x => x.Id, 0)
                                                   .Create();

            _postServiceMock.Setup(x => x.AddRequestAsync(postDto, It.IsAny <WsClient>())).ReturnsAsync(expectedResult);
            _cacheServiceMock.Setup(x => x.GetWsClientsAsync()).ReturnsAsync(expectedWsClients);
            _controller.ControllerContext.HttpContext.Request.Headers["Hash"] = _fixture.Create <string>();

            // Act
            IActionResult actionResult = await _controller.AddPost(postDto);

            // Assert
            actionResult.Should().NotBeNull().And.BeOfType <ObjectResult>();

            _cacheServiceMock.Verify(x => x.GetWsClientsAsync(), Times.Once);
            _postServiceMock.Verify(x => x.AddRequestAsync(postDto, It.IsAny <WsClient>()), Times.Once);
            _loggerMock.Verify(m => m.Log(
                                   LogLevel.Error,
                                   0,
                                   It.Is <FormattedLogValues>(v => v.ToString()
                                                              .Contains($"Error for: '{postDto.GetType().Name}' -> Message: '{expectedResult.Message}'")),
                                   null,
                                   It.IsAny <Func <object, Exception, string> >()
                                   ));

            var objectResult = actionResult as ObjectResult;

            objectResult.StatusCode.Should().Be(StatusCodes.Status500InternalServerError);
            objectResult.Value.Should().NotBeNull().And.BeOfType(typeof(IdResultDto <long>));
            var idResultDto = objectResult.Value as IdResultDto <long>;

            idResultDto.IsOk.Should().Be(false);
            idResultDto.Message.Should().NotBeNull().And.Be(expectedResult.Message);
            idResultDto.Id.Should().Be(0, because: "Post shouldn't be added");
        }
Exemplo n.º 3
0
        public async void AddPostNotValid()
        {
            // Arrange
            PostDto   postDto          = new PostDto();
            string    expectedErrorMsg = "Validation errors: Null User data.;Null Content data";
            ResultDto expectedResult   = new ResultDto()
            {
                Message = expectedErrorMsg
            };

            _mapperMock.Setup(x => x.Map <ResultDto>(It.IsAny <MessageListResult>()))
            .Returns(expectedResult);

            // Act
            IActionResult actionResult = await _controller.AddPost(postDto);

            // Assert
            _cacheServiceMock.Verify(x => x.GetWsClientsAsync(), Times.Never);
            _postServiceMock.Verify(x => x.AddRequestAsync(postDto, It.IsAny <WsClient>()), Times.Never);
            _loggerMock.Verify(m => m.Log(
                                   LogLevel.Error,
                                   0,
                                   It.Is <FormattedLogValues>(v => v.ToString()
                                                              .Contains($"ValidErr for '{postDto.GetType().Name}': {expectedErrorMsg}")),
                                   null,
                                   It.IsAny <Func <object, Exception, string> >()
                                   ));
            _mapperMock.Verify(x => x.Map <ResultDto>(It.IsAny <MessageListResult>()), Times.Once);

            actionResult.Should().NotBeNull().And.BeOfType <BadRequestObjectResult>();

            var badRequestResult = actionResult as BadRequestObjectResult;

            badRequestResult.StatusCode.Should().Be(StatusCodes.Status400BadRequest);
            badRequestResult.Value.Should().NotBeNull().And.BeOfType <ResultDto>();
            var resultDto = badRequestResult.Value as ResultDto;

            resultDto.IsOk.Should().Be(false);
            resultDto.Message.Should().NotBeNull().And.Be(expectedErrorMsg);
        }