public void UnitTestMeatList() { using (var context = new GroceryCartContext(DbOptionsFactory.DbContextOptions)) { // 1. Create instance of fake repo using IProductRepository interface. var mockMeatRepo = new Mock <IMeatRepository>(); // 2. Set up return data for ProductList() method. mockMeatRepo.Setup(mpr => mpr.MeatList()) .Returns(new List <Meat> { new Meat(), new Meat(), new Meat() }); // 3. Define controller instance with mock repository instance. var controller = new MeatController(context, mockMeatRepo.Object); // 4. Make your test Assertions // Check if it returns a view var result = Assert.IsType <ViewResult>(controller.Index()); // Check that the model returned to the view is 'List<Product>'. var model = Assert.IsType <List <Meat> >(result.Model); // Ensure count of objects is 3. int expected = 3; int actual = model.Count; Assert.Equal(expected, actual); } }
public void IndexViewHas5Meats() { using (var context = new GroceryCartContext(DbOptionsFactory.DbContextOptions)) { var meatRepository = new MeatRepo(context); var controller = new MeatController(context, meatRepository); int expected = 5; var viewResult = Assert.IsType <ViewResult>(controller.Index()); var model = Assert.IsType <List <Meat> >(viewResult.Model); int actual = model.Count; Assert.Equal(expected, actual); } }