public async Task PersonByName_Should_Call_IPersonService_GetPersonByName_And_Return_NotFound_If_IPersonService_GetPersonByName_Returns_Null() { PersonControllerMock mock = PersonControllerMock.Create(); mock.PersonService.Setup(service => service.GetPersonByName(It.IsAny <string>())).ReturnsAsync(() => null); IActionResult actionResult = await mock.PersonByName("Fırat Özbolat"); var notFoundResult = actionResult as NotFoundResult; Assert.NotNull(notFoundResult); mock.PersonService.Verify(service => service.GetPersonByName(It.IsAny <string>()), Times.Once); }
public async Task PersonByName_Should_Return_BadRequest_If_ModelState_Is_Invalid() { PersonControllerMock mock = PersonControllerMock.Create(); mock.ModelState.AddModelError("test", "test"); IActionResult actionResult = await mock.PersonByName("Deniz Özgen"); var badRequestObjectResult = actionResult as BadRequestObjectResult; Assert.NotNull(badRequestObjectResult); var serializableError = badRequestObjectResult.Value as SerializableError; Assert.NotNull(serializableError); Assert.True(((string[])serializableError["test"])[0] == "test"); mock.PersonService.Verify(service => service.GetPersonByName(It.IsAny <string>()), Times.Never); }
public async Task PersonByName_Should_Call_IPersonService_GetPersonByName_And_Return_Ok() { PersonControllerMock mock = PersonControllerMock.Create(); var person = "Ahmet Erdem Kahveci"; mock.PersonService.Setup(service => service.GetPersonByName(It.Is <string>(i => i == person))).ReturnsAsync(() => new Person()); IActionResult actionResult = await mock.PersonByName(person); var okObjectResult = actionResult as OkObjectResult; Assert.NotNull(okObjectResult); var personModel = okObjectResult.Value as Person; Assert.NotNull(personModel); mock.PersonService.Verify(service => service.GetPersonByName(It.IsAny <string>()), Times.Once); }