public async void AddClientAsync_Returns_NotFound_404_When_Create_With_null_Client() { //Configure Repository Mock _mockRepository.Setup(repo => repo.AddTAsync(null)).ReturnsAsync(0); //instantiate the controller, and call the method var controller = new ClientsController(_mockRepository.Object, _mapper, _logger); //Create Custom ControllerContext and add it to Controller for logging in the Controller in case of error controller.ControllerContext = new ControllerContextModel(); //Call the SUT method - returns ActionResult<Client> type var actionResult = await controller.AddClientAsync(null); // (new Client()); //Assert the result Assert.NotNull(actionResult); var result = actionResult.Result as NotFoundObjectResult; var statusCode = result.StatusCode; var message = result.Value; //Assert message Assert.Equal("No Client was created", message); //Assert StatusCode Assert.Equal(404, statusCode); }
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>()); }