Ejemplo n.º 1
0
        public async Task IndexListAllProducts()
        {
            //Arrange
            // All contexts that share the same service provider will share the same InMemory database
            var options = CreateNewContextOptions();

            // Insert seed data into the database using one instance of the context
            using (var context = new WebshopRepository(options))
            {
                context.ProductCategories.Add(new ProductCategory {
                    ProductCategoryId = 3, ProductCategoryName = "frukt"
                });

                context.Products.Add(new Product {
                    ProductId = 1, Price = 55, PictureURL = "gwgwgw", ProductCategoryId = 3
                });
                context.Products.Add(new Product {
                    ProductId = 2, Price = 69, ProductCategoryId = 3
                });
                context.Products.Add(new Product {
                    ProductId = 3, Price = 109, ProductCategoryId = 3
                });
                context.SaveChanges();

                context.ProductTranslations.Add(new ProductTranslation {
                    ProductName = "hallon", ProductId = 1, Language = "sv", ProductDescription = "hejhå"
                });
                context.ProductTranslations.Add(new ProductTranslation {
                    ProductName = "banan", ProductId = 2, Language = "sv", ProductDescription = "hejhå"
                });
                context.ProductTranslations.Add(new ProductTranslation {
                    ProductName = "päron", ProductId = 3, Language = "sv", ProductDescription = "hejhå"
                });

                context.SaveChanges();
            }

            // Use a clean instance of the context to run the test

            using (var context = new WebshopRepository(options))
            {
                var service = new ProductsController(context);

                //Act
                var result = await service.Index(null);

                //Assert

                var viewResult = Assert.IsType <ViewResult>(result);
                var model      = Assert.IsAssignableFrom <IEnumerable <Lesson6.ViewModels.ProductViewModel> >(viewResult.ViewData.Model);
                var transModel = Assert.IsAssignableFrom <IEnumerable <Lesson6.ViewModels.ProductViewModel> >(viewResult.ViewData.Model);


                Assert.Equal(3, model.Count());
                Assert.Equal("hallon", transModel.ElementAt(0).ProductName);
            }
        }
Ejemplo n.º 2
0
        public async Task CreateReturnViewDataWhenModelstateIsInvalid()
        {
            //Arrenge
            var options = CreateNewContextOptions();

            using (var context = new WebshopRepository(options))
            {
                context.ProductCategories.Add(new ProductCategory {
                    ProductCategoryId = 3, ProductCategoryName = "frukt"
                });

                context.Products.Add(new Product {
                    ProductId = 1, Price = 55, PictureURL = "gwgwgw", ProductCategoryId = 3
                });
                context.Products.Add(new Product {
                    ProductId = 2, Price = 69, ProductCategoryId = 3
                });
                context.Products.Add(new Product {
                    ProductId = 3, Price = 109, ProductCategoryId = 3
                });
                context.SaveChanges();



                context.ProductTranslations.Add(new ProductTranslation {
                    ProductName = "hallon", ProductId = 1, Language = "sv", ProductDescription = "hejhå"
                });
                context.ProductTranslations.Add(new ProductTranslation {
                    ProductName = "banan", ProductId = 2, Language = "sv", ProductDescription = "hejhå"
                });
                context.ProductTranslations.Add(new ProductTranslation {
                    ProductName = "päron", ProductId = 3, Language = "sv", ProductDescription = "hejhå"
                });

                context.SaveChanges();
            }

            using (var context = new WebshopRepository(options))
            {
                var service = new ProductsController(context);
                service.ModelState.AddModelError("Error1", "Error2");


                //Act
                var product = new ProductViewModel {
                    ProductName = "testtest", ProductId = 3, PictureURL = "wgweew", Price = 49, ProductCategoryId = 3, ProductDescription = "testkategori"
                };
                var result = await service.Create(product);

                //Assert

                var viewResult = Assert.IsType <ViewResult>(result);
                var model      = Assert.IsAssignableFrom <Lesson6.ViewModels.ProductViewModel>(viewResult.ViewData.Model);
            }
        }
Ejemplo n.º 3
0
        public async Task DetailsReturnNotFoundWhenIdIsNull()
        {
            //Arrange
            // All contexts that share the same service provider will share the same InMemory database
            var options = CreateNewContextOptions();

            // Insert seed data into the database using one instance of the context
            using (var context = new WebshopRepository(options))
            {
                context.ProductCategories.Add(new ProductCategory {
                    ProductCategoryId = 3, ProductCategoryName = "frukt"
                });

                context.Products.Add(new Product {
                    ProductId = 1, Price = 55, PictureURL = "gwgwgw", ProductCategoryId = 3
                });
                context.Products.Add(new Product {
                    ProductId = 2, Price = 69, ProductCategoryId = 3
                });
                context.Products.Add(new Product {
                    ProductId = 3, Price = 109, ProductCategoryId = 3
                });
                context.SaveChanges();



                context.ProductTranslations.Add(new ProductTranslation {
                    ProductName = "hallon", ProductId = 1, Language = "sv", ProductDescription = "hejhå"
                });
                context.ProductTranslations.Add(new ProductTranslation {
                    ProductName = "banan", ProductId = 2, Language = "sv", ProductDescription = "hejhå"
                });
                context.ProductTranslations.Add(new ProductTranslation {
                    ProductName = "päron", ProductId = 3, Language = "sv", ProductDescription = "hejhå"
                });

                context.SaveChanges();
            }

            // Use a clean instance of the context to run the test
            using (var context = new WebshopRepository(options))
            {
                var service = new ProductsController(context);

                //Act
                var result = await service.Details(id : null);

                //Assert

                var notFoundResult = Assert.IsType <NotFoundResult>(result);
                Assert.IsType <NotFoundResult>(notFoundResult);
            }
        }
Ejemplo n.º 4
0
        public async Task SearchIndexProducts()
        {
            //Arrange
            var options = CreateNewContextOptions();

            using (var context = new WebshopRepository(options))
            {
                context.ProductCategories.Add(new ProductCategory {
                    ProductCategoryId = 3, ProductCategoryName = "frukt"
                });

                context.Products.Add(new Product {
                    ProductId = 1, Price = 55, PictureURL = "gwgwgw", ProductCategoryId = 3
                });
                context.Products.Add(new Product {
                    ProductId = 2, Price = 69, ProductCategoryId = 3
                });
                context.Products.Add(new Product {
                    ProductId = 3, Price = 109, ProductCategoryId = 3
                });
                context.SaveChanges();



                context.ProductTranslations.Add(new ProductTranslation {
                    ProductName = "hallon", ProductId = 1, Language = "sv", ProductDescription = "hejhå"
                });
                context.ProductTranslations.Add(new ProductTranslation {
                    ProductName = "banan", ProductId = 2, Language = "sv", ProductDescription = "hejhå"
                });
                context.ProductTranslations.Add(new ProductTranslation {
                    ProductName = "päron", ProductId = 3, Language = "sv", ProductDescription = "hejhå"
                });

                context.SaveChanges();
            }

            using (var context = new WebshopRepository(options))
            {
                var service = new ProductsController(context);

                //Act
                var result = await service.Index("hallon");

                //Assert
                var viewResult = Assert.IsType <ViewResult>(result);
                var model      = Assert.IsAssignableFrom <IEnumerable <Lesson6.ViewModels.ProductViewModel> >(viewResult.ViewData.Model);

                Assert.Equal(1, model.Count());
                Assert.Equal("hallon", model.ElementAt(0).ProductName);
            }
        }
Ejemplo n.º 5
0
        public async Task CreateReturnRedirectToActionIndex()
        {
            //Arrange
            var option = CreateNewContextOptions();

            using (var context = new WebshopRepository(option))
            {
                //context.ProductCategories.Add(new ProductCategory { ProductCategoryId = 3, ProductCategoryName = "frukt" });

                //context.Products.Add(new Product { ProductId = 1, Price = 55, PictureURL = "gwgwgw", ProductCategoryId = 3 });
                //context.Products.Add(new Product { ProductId = 2, Price = 69, ProductCategoryId = 3 });
                //context.Products.Add(new Product { ProductId = 3, Price = 109, ProductCategoryId = 3 });
                //context.SaveChanges();



                //context.ProductTranslations.Add(new ProductTranslation { ProductName = "hallon", ProductId = 1, Language = "sv", ProductDescription = "hejhå" });
                //context.ProductTranslations.Add(new ProductTranslation { ProductName = "banan", ProductId = 2, Language = "sv", ProductDescription = "hejhå" });
                //context.ProductTranslations.Add(new ProductTranslation { ProductName = "päron", ProductId = 3, Language = "sv", ProductDescription = "hejhå" });

                //context.SaveChanges();
            }

            using (var context = new WebshopRepository(option))
            {
                var service = new ProductsController(context);

                //Act
                var product = new ProductViewModel {
                    ProductName = "testtest", ProductId = 1, PictureURL = "wgweew", Price = 49, ProductCategoryId = 3, ProductDescription = "testkategori"
                };
                var result = await service.Create(product);



                //Assert
                var redirectToActionResult = Assert.IsType <RedirectToActionResult>(result);

                Assert.Equal("Index", redirectToActionResult.ActionName);
            }
        }
Ejemplo n.º 6
0
 public ProductCategoriesController(WebshopRepository context)
 {
     _context = context;
 }
Ejemplo n.º 7
0
 public ShoppingcartViewComponent(WebshopRepository context)
 {
     _context = context;
 }
Ejemplo n.º 8
0
 public ShoppingCartController(WebshopRepository context)
 {
     _context = context;
 }
Ejemplo n.º 9
0
 public ProductsController(WebshopRepository context /*, IStringLocalizer<WebshopRepository> localizer*/)
 {
     _context = context;
     // _localizer = localizer;
 }
Ejemplo n.º 10
0
 public WebshopAPIController(WebshopRepository context)
 {
     _context = context;
 }