Exemplo n.º 1
0
        public void DeleteViewResultWithViewModel()
        {
            //Won't work, due to it use global Session["ShoppingCartSession"]
            // Arrange
            ShoppingCart shoppingCart = new ShoppingCart()
            {
                ProductId = 1,
                Unit      = 1
            };
            List <ShoppingCart> shoppingCartList = new List <ShoppingCart>();
            var mockLogger   = new Mock <ILogger <HomeController> >();
            var mockCateRepo = new Mock <ICategoryRepository>();
            var mockProdRepo = new Mock <IProductRepository>();

            var session = new MockHttpSession();
            Mock <HttpContext> mockHttpContext = new Mock <HttpContext>();
            MockHttpSession    mockSession     = new MockHttpSession();

            mockSession["ShoppingCartSession"] = new List <ShoppingCart> {
                shoppingCart
            };

            var request = new Mock <HttpRequest>();

            request.Setup(x => x.Scheme).Returns("http");
            request.Setup(x => x.Host).Returns(HostString.FromUriComponent("http://localhost:44331"));
            request.Setup(x => x.PathBase).Returns(PathString.FromUriComponent("/home"));

            var httpContext = Moq.Mock.Of <HttpContext>(_ =>
                                                        _.Request == request.Object
                                                        );

            //Controller needs a controller context
            var controllerContext = new ControllerContext()
            {
                HttpContext = httpContext,
            };
            //assign context to controller
            var controller = new HomeController(mockLogger.Object, mockProdRepo.Object, mockCateRepo.Object)
            {
                ControllerContext = controllerContext,
            };



            //Action
            //var result = controller.RemoveFromCart(1);

            //
            //var viewResult = Assert.IsType<ViewResult>(result);
            //Assert.Null(viewResult);
            //Assert.Null(viewResult.ViewData.Model);
            //Assert.IsType<ShoppingCart>(viewResult.ViewData.Model);
        }
Exemplo n.º 2
0
        public void DetailsViewResultWithViewModel(int detailId)
        {
            // Arrange
            HomeVM homeVM = new HomeVM()
            {
                Products   = GetTestProduct(),
                Categories = GetTestCategory()
            };
            List <ShoppingCart> shoppingCartList = new List <ShoppingCart>();
            var mockLogger   = new Mock <ILogger <HomeController> >();
            var mockCateRepo = new Mock <ICategoryRepository>();
            var mockProdRepo = new Mock <IProductRepository>();
            var categories   = (IEnumerable <Category>)GetTestCategory();

            var prod = GetTestProduct().FirstOrDefault();
            var cate = GetTestCategory().FirstOrDefault();

            DetailsVM detailVM = new DetailsVM()
            {
                ExistsInCart = false,
                Product      = prod
            };

            mockCateRepo.Setup(x => x.GetAll(null, null, null, true)).Returns(homeVM.Categories);
            mockProdRepo.Setup(x => x.GetAll(null, null, "Category,ApplicationType", true)).Returns(homeVM.Products);
            var controller = new HomeController(mockLogger.Object, mockProdRepo.Object, mockCateRepo.Object);

            controller.ControllerContext.HttpContext =
                new DefaultHttpContext();
            var session = new MockHttpSession();
            Mock <HttpContext> mockHttpContext = new Mock <HttpContext>();
            MockHttpSession    mockSession     = new MockHttpSession();

            mockSession["ShoppingCartSession"] = null;
            mockHttpContext.Setup(s => s.Session).Returns(mockSession);
            //controller.ControllerContext.HttpContext = (HttpContext)mockHttpContext.Object;
            controller.ControllerContext.HttpContext.Session = mockSession;


            //Action
            var result = controller.Details(detailId);

            // Assert correct non-null View returned with no Model
            var viewResult = Assert.IsType <ViewResult>(result);

            Assert.NotNull(viewResult);
            Assert.NotNull(viewResult.ViewData.Model);
            var viewResultValue = Assert.IsAssignableFrom <DetailsVM>(viewResult.ViewData.Model);

            Assert.IsType <DetailsVM>(viewResult.ViewData.Model);
        }