Esempio n. 1
0
        public void UnitTestVegList()
        {
            using (var context = new GroceryCartContext(DbOptionsFactory.DbContextOptions))
            {
                // 1. Create instance of fake repo using IProductRepository interface.
                var mockVegRepo = new Mock <IVegRepository>();

                // 2. Set up return data for ProductList() method.
                mockVegRepo.Setup(mpr => mpr.VegList())
                .Returns(new List <Vegetables> {
                    new Vegetables(), new Vegetables(), new Vegetables()
                });

                // 3. Define controller instance with mock repository instance.
                var controller = new VegetablesController(context, mockVegRepo.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 <Vegetables> >(result.Model);

                // Ensure count of objects is 3.
                int expected = 3;
                int actual   = model.Count;
                Assert.Equal(expected, actual);
            }
        }
Esempio n. 2
0
 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);
     }
 }