Example #1
0
        public async void UpdateClientAsync_Updates_One_Client_Returns_200_And_Client_Updated(int id)
        {
            //declare a Client
            var       expectedClient = ClientsData.getTestClients().First <Client>(cl => cl.Id == id);
            ClientDTO expectedDTO    = _mapper.Map <ClientDTO>(expectedClient);

            //set repo return for getting the object to update
            _mockRepository.Setup(repo => repo.GetOneByAsync(cl => cl.Id == expectedClient.Id))
            .ReturnsAsync(expectedClient);

            //set mockRepo return for Update action
            _mockRepository.Setup(repo => repo.UpdateTAsync(expectedClient)).ReturnsAsync(1);

            //instantiate the controller, passing the repo object
            var controller = new ClientsController(_mockRepository.Object, _mapper, _logger);

            //Call the SUT method - returns ActionResult<Client> type
            var actionResult = await controller.UpdateClientAsync(expectedClient.Id, expectedClient);

            //Get the int result from the posted ActionResult
            var       okObjectResult = actionResult.Result as OkObjectResult;
            var       statusCode     = okObjectResult.StatusCode;
            ClientDTO actualDTO      = okObjectResult.Value as ClientDTO;

            //Assert the result
            Assert.NotNull(actionResult);

            //Validate StatusCode
            Assert.Equal(200, statusCode);

            //Validate the actual Client
            actualDTO.Should().BeEquivalentTo(expectedDTO, options => options.ComparingByMembers <ClientDTO>());
        }
Example #2
0
        public async void AddClientAsync_Creates_One_Client_Returns_201_And_Client_Created(int id)
        {
            //get a Client and set expected DTO
            var       expectedClient = ClientsData.getTestClients().First <Client>(cl => cl.Id == id);
            ClientDTO expectedDTO    = _mapper.Map <ClientDTO>(expectedClient);

            //set mockRepo return for Add action
            _mockRepository.Setup(repo => repo.AddTAsync(expectedClient)).ReturnsAsync(1);

            //set repo return for getting the newly created object
            _mockRepository.Setup(repo => repo.GetOneByAsync(cl =>
                                                             cl.Name == expectedClient.Name &&
                                                             cl.Description == expectedClient.Description &&
                                                             cl.Website == expectedClient.Website))
            .ReturnsAsync(expectedClient);

            //instantiate the controller, passing the repo object
            var controller = new ClientsController(_mockRepository.Object, _mapper, _logger);

            //Call the SUT method - returns ActionResult<Client> type
            var actionResult = await controller.AddClientAsync(expectedClient);

            //Get the int result from the posted ActionResult
            var       createdResult = actionResult.Result as CreatedResult;
            var       statusCode    = createdResult.StatusCode;
            ClientDTO actualDTO     = createdResult.Value as ClientDTO;

            //Assert the result
            Assert.NotNull(actionResult);

            //Validate the return is 1 Client created
            Assert.Equal(201, statusCode);

            //Validate the actual Client
            actualDTO.Should().BeEquivalentTo(expectedDTO, options => options.ComparingByMembers <ClientDTO>());
        }