//The Method checks the returned result is NotFoundReult 404, when PersonInfo is not found //The Method under test calls FindAsync(fake id) gets null and returns NotFoundResult public async Task GetPersonInfo_NotFoundResult_404_When_PersonInfo_not_found() { //Arrange var fakeId = 88; var _contextMock = new Mock <PeopleDWContext>(); _contextMock .Setup(context => context.PersonInfo.FindAsync(It.IsAny <long>())) .Returns(Task.FromResult((PersonInfo)null)); //Act var personInfoesController = new PersonInfoesController(_contextMock.Object); var actionResult = await personInfoesController.GetPersonInfo(fakeId); //Assert var notFoundResult = Assert.IsType <NotFoundResult>(actionResult); Assert.Equal(404, notFoundResult.StatusCode); }
//The Method checks the returned result on success. it should be OkObjectResault //The Method checks the returned object wrapped in the result. it should be PersonInfo //The Method under test calls FindAsync(long) and returns PersonInfo wrapped in the ActionResult public async Task GetPersonInfo_return_PersonInfo_wrapped_OkObjectResault_When_Success() { //Arrange var trueId = 1; var testPersonInfo = GetTestPersonInfoAsync(); var _contextMock = new Mock <PeopleDWContext>(); _contextMock .Setup(context => context.PersonInfo.FindAsync(It.IsAny <long>())) .Returns(testPersonInfo); //Act var personInfoesController = new PersonInfoesController(_contextMock.Object); var actionResult = await personInfoesController.GetPersonInfo(trueId); //Assert var okObjectResult = Assert.IsType <OkObjectResult>(actionResult); Assert.IsAssignableFrom <PersonInfo>(okObjectResult.Value); }
//The Method checks the returned result is BadRequestObjectResult 400, when ControllerBase.ModelState has an error public async Task GetPersonInfo_BadRequestObjectResult_400_When_ModelState_not_valid() { //Arrange var anyId = 1; var _contextMock = new Mock <PeopleDWContext>(); //_contextMock // .Setup(context => context.PersonInfo.FindAsync(It.IsAny<long>())) // .Returns(GetTestPersonInfoAsync()); //Act var personInfoesController = new PersonInfoesController(_contextMock.Object); personInfoesController.ModelState.AddModelError("Key", "Test_Error"); var actionResult = await personInfoesController.GetPersonInfo(anyId); //Assert var badRequestObjectResult = Assert.IsType <BadRequestObjectResult>(actionResult); Assert.Equal(400, badRequestObjectResult.StatusCode); }
//The Method checks the found id must be equal to the id of the returned object //The Method under test calls FindAsync(long) and returns PersonInfo wrapped in the ActionResult public async Task GetPersonInfo_returned_Id_people_equals_Id_people_found_When_Success() { //Arrange var trueId = 1; var testPersonInfo = GetTestPersonInfoAsync(); var _contextMock = new Mock <PeopleDWContext>(); _contextMock .Setup(context => context.PersonInfo.FindAsync(It.IsAny <long>())) .Returns(testPersonInfo); //Act var personInfoesController = new PersonInfoesController(_contextMock.Object); var returnPersonInfo = (PersonInfo) ((OkObjectResult)await personInfoesController.GetPersonInfo(trueId)) .Value; //Assert Assert.True(testPersonInfo.Result.IdPeople.Equals(returnPersonInfo.IdPeople)); }