public void OnGet_VerifyCorrectServiceMethodIsCalled() { //Arrange var mockService = new Mock <IListPoemsService>(); mockService.Setup(s => s.ListPoemsByTitle()).Returns(new List <PoemDTO>()); //Act var listModel = new ListPoemsByTitleModel(mockService.Object); var testResult = listModel.OnGet(); //Assert mockService.Verify(s => s.ListPoemsByTitle()); }
public void OnGet_VerifyPageIsReturnedAndPoemListIsSet() { //Arrange var poems = new PoemBuilder().Default().BuildCollection(size: 3); var poemDTOList = poems.Select(p => PoemDTO.CreateDTOFromEntity(p)); var mockService = new Mock <IListPoemsService>(); mockService.Setup(s => s.ListPoemsByTitle()).Returns(poemDTOList); //Act var listModel = new ListPoemsByTitleModel(mockService.Object); var testResult = listModel.OnGet(); //Assert Assert.IsType <PageResult>(testResult); Assert.Equal(poemDTOList.Count(), listModel.Poems.Count()); }
public void OnGet_InvalidModelState_VerifyServiceMethodIsNotCalledAndRedirect() { //Arrange var mockService = new Mock <IListPoemsService>(); mockService.Setup(s => s.ListPoemsByTitle()); var listModel = new ListPoemsByTitleModel(mockService.Object); listModel.ModelState.AddModelError("test", "invalid state"); //Act var testResult = listModel.OnGet(); //Assert mockService.Verify(s => s.ListPoemsByTitle(), Times.Never); Assert.IsType <RedirectToPageResult>(testResult); var redirectToPage = (RedirectToPageResult)testResult; Assert.Equal(REDIRECT_PAGE_NAME_WHEN_ERROR, redirectToPage.PageName); }