public async Task AddClientReturnOkObjectResultCreatingValidClient() { // Arrange ClientRequest request = new ClientRequest() { Name = "New Client" }; _mockRepository.Setup(repo => repo.Add(It.IsAny <Client>())).Verifiable(); _mockRepository.Setup(repo => repo.SaveAsync()).Verifiable(); // Act var controller = new ClientController(_mockLogger.Object, _mockRepository.Object); var result = await controller.AddAsync(request); // Assert _mockRepository.Verify(repo => repo.Add(It.IsAny <Client>()), Times.Once); _mockRepository.Verify(repo => repo.SaveAsync(), Times.Once); var okObjectResult = Assert.IsType <OkObjectResult>(result); var response = Assert.IsType <Response>(okObjectResult?.Value); var client = Assert.IsType <Client>(response?.Data); Assert.Equal(request.Name, client.Name); Assert.True(response.Success); }
public async Task AddClientReturnBadRequestCreatingNullClient() { // Arrange _mockRepository.Setup(repo => repo.Add(null)).Verifiable(); _mockRepository.Setup(repo => repo.SaveAsync()).Verifiable(); // Act var controller = new ClientController(_mockLogger.Object, _mockRepository.Object); var result = await controller.AddAsync(null); // Assert _mockRepository.Verify(repo => repo.Add(It.IsAny <Client>()), Times.Never); _mockRepository.Verify(repo => repo.SaveAsync(), Times.Never); Assert.IsType <BadRequestResult>(result); }
public async Task AddClientReturnOkObjectResultWhenRepositoryThrowException() { // Arrange ClientRequest request = new ClientRequest() { Name = "New Client" }; _mockRepository.Setup(repo => repo.Add(It.IsAny <Client>())).Throws(new Exception("Error")); _mockRepository.Setup(repo => repo.SaveAsync()).Verifiable(); // Act var controller = new ClientController(_mockLogger.Object, _mockRepository.Object); var result = await controller.AddAsync(request); // Assert _mockRepository.Verify(repo => repo.Add(It.IsAny <Client>()), Times.Once); _mockRepository.Verify(repo => repo.SaveAsync(), Times.Never); var okObjectResult = Assert.IsType <OkObjectResult>(result); var response = Assert.IsType <Response>(okObjectResult?.Value); Assert.Null(response?.Data); Assert.False(response?.Success); }