Beispiel #1
0
        public void Constroller_Return_All_Product_To_View()
        {
            //Arrange
            Mock <IProductRepository> MoqProductRepository = new Mock <IProductRepository>();

            MoqProductRepository.Setup(e => e.AllProduct()).Returns(
                new List <Product>()
            {
                new Product {
                    Id = 0, Name = "Product HK"
                },
                new Product {
                    Id = 1, Name = "Product CN"
                }
            }
                );
            DummyController Controller = new DummyController(null);

            //Act
            ViewResult VR    = Controller.GetAllAction() as ViewResult;
            var        Model = VR.Model as List <Product>;

            //Assert
            Assert.IsTrue(Model.Count == 2, "Expected Product Count is 2 ; Actual is" + Model.Count);
            Assert.IsTrue(Model[0].Name == "Product HK", "Expected 1st Product Name is Product HK ; Actual is " + Model[0].Name);
        }
        public void GetAllAction_WhenCall_ShouldReturnProductInFutureOnlyToView()
        {
            //Arranage
            Product[] products = new[] { new Product {
                                             Id = 100, CreateDate = DateTime.Now.AddDays(1), Name = "Dummy1", Category = 0, Price = 0
                                         },
                                         new Product {
                                             Id = 101, CreateDate = DateTime.Now.AddDays(2), Name = "Dummy2", Category = 0, Price = 0
                                         },
                                         new Product {
                                             Id = 102, CreateDate = DateTime.Now.AddDays(-2), Name = "DummyObsolete", Category = 0, Price = 0
                                         } };

            Category category = new Category {
                Id = 0, Name = "Dummy Category"
            };

            try
            {
                context.Categories.Add(category);
                context.Products.AddRange(products);

                context.SaveChanges();

                //Act
                DummyController controller = new DummyController(new UnitOfWork());
                var             result     = controller.GetAllAction();

                //Assert
                var resultProducts = result.ViewData.Model as List <Wrox.BooksRead.Web.Models.Product>;
                resultProducts.Should().HaveCount(2);
            }
            finally
            {
                //Tear Down
                context.Products.RemoveRange(products);
                context.Categories.Remove(category);
                context.SaveChanges();
            }
        }