public void TestIndexMethodReturnsObjects() { var mockRepo = new Mock<ISpeciesRepository>(); mockRepo.Setup(repo => repo.Get()) .Returns(DataTestService.GetTestSpecies()); var controller = new SpeciesController(mockRepo.Object); // Act var result = controller.Index(); // Assert var viewResult = Assert.IsType<ViewResult>(result); var model = Assert.IsAssignableFrom<IEnumerable<Species>>( viewResult.ViewData.Model); Assert.Equal(2, model.Count()); }
public void TestIndexMethodReturnsObjects() { // Arrange var mockRepo = new Mock <ISpeciesRepository>(); //create mock repo mockRepo.Setup(repo => repo.Get()) .Returns(DataTestService.GetTestSpecies()); //Creates test species in mock repo and sends in the hardcoded test data to the tested class var controller = new SpeciesController(mockRepo.Object); //Use the species controller // Act var result = controller.Index(); //We are not doing async so no await. This will run the method in question // Assert var viewResult = Assert.IsType <ViewResult>(result); //Expect a view to be the output of the method var model = Assert.IsAssignableFrom <IEnumerable <Species> >( //asserts that we get a Species back viewResult.ViewData.Model); Assert.Equal(2, model.Count()); //Asserts that we get two items back from the list from the model }