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); } }
// Initialize context when controller is created. public HomeController(GroceryCartContext db, IHttpContextAccessor httpContextAccessor, IOptions <EmailSettings> _emailSettings) { this.db = db; Seeder seeder = new Seeder(db); this._httpContextAccessor = httpContextAccessor; this._emailSettings = _emailSettings.Value; }
public void IndexViewHas5Veggies() { using (var context = new GroceryCartContext(DbOptionsFactory.DbContextOptions)) { var veggiesRepository = new VegRepo(context); var controller = new VegetablesController(context, veggiesRepository); int expected = 5; var viewResult = Assert.IsType <ViewResult>(controller.Index()); var model = Assert.IsType <List <Vegetables> >(viewResult.Model); int actual = model.Count; Assert.Equal(expected, actual); } }
public EmployeeController(GroceryCartContext db) { this.db = db; }
public EmployeeVMRepo(GroceryCartContext db) { this.db = db; }
public CustomerVMRepo(GroceryCartContext db) { this.db = db; }
public VegetablesController(GroceryCartContext db, IVegRepository vegRepo) { this.db = db; _vegRepo = vegRepo; }
public GrainController(GroceryCartContext db, IGrainRepository grainRepo) { this.db = db; _grainRepo = grainRepo; }
public CustomerController(GroceryCartContext db) { this.db = db; }
public MeatRepo(GroceryCartContext db) { this.db = db; }
public GrainRepo(GroceryCartContext db) { this.db = db; }
public FruitsRepo(GroceryCartContext db) { this.db = db; }
public VegRepo(GroceryCartContext db) { this.db = db; }
public MeatController(GroceryCartContext db, IMeatRepository meatRepo) { this.db = db; _meatRepo = meatRepo; }
public DrinksRepo(GroceryCartContext db) { this.db = db; }
public DrinksController(GroceryCartContext db) { this.db = db; }
public FruitController(GroceryCartContext db, IFruitsRepository fruitRepo) { this.db = db; _fruitRepo = fruitRepo; }
public GroceryCart(GroceryCartContext db) { this.db = db; }