Exemple #1
0
        public async Task <PagedProductsDTO> Find(ProductFilterDTO filter)
        {
            var productQuery = _repo.GetQuery();

            productQuery = productQuery
                           .Skip((filter.PageNumber - 1) * filter.PageCapacity)
                           .Take(filter.PageCapacity);

            var products = await productQuery.ToListAsync();

            PagedProductsDTO result = new PagedProductsDTO()
            {
                PageNumber = filter.PageNumber,
                Products   = _mapper.Map <IEnumerable <Product>, IEnumerable <ProductDTO> >(products)
            };

            return(result);
        }
        public void Shop_Returns_Correct_View()
        {
            var resultProducts = new PagedProductsDTO
            {
                TotalCount = 3,
                Products   = new[]
                {
                    new ProductDTO
                    {
                        Id       = 1,
                        Name     = "Product 1",
                        Order    = 0,
                        ImageUrl = "Product1.png",
                        Price    = 10m,
                        Brand    = new BrandDTO {
                            Id = 1, Name = "Brand of Product 1"
                        }
                    },
                    new ProductDTO
                    {
                        Id       = 2,
                        Name     = "Product 2",
                        Order    = 0,
                        ImageUrl = "Product2.png",
                        Price    = 10m,
                        Brand    = new BrandDTO {
                            Id = 1, Name = "Brand of Product 2"
                        }
                    },
                    new ProductDTO
                    {
                        Id       = 3,
                        Name     = "Product 3",
                        Order    = 0,
                        ImageUrl = "Product3.png",
                        Price    = 10m,
                        Brand    = new BrandDTO {
                            Id = 1, Name = "Brand of Product 3"
                        }
                    }
                }
            };

            var productDataMock = new Mock <IProductData>();

            productDataMock
            .Setup(p => p.GetProducts(It.IsAny <ProductFilter>()))
            .Returns <ProductFilter>(filter => resultProducts);

            var configurationMock = new Mock <IConfiguration>();
            var controller        = new CatalogController(productDataMock.Object, configurationMock.Object);

            var expected = new { SectionId = 1, BrandId = 5 };

            var result = controller.Shop(expected.SectionId, expected.BrandId);

            var viewResult = Assert.IsType <ViewResult>(result);
            var model      = Assert.IsAssignableFrom <CatalogViewModel>(viewResult.ViewData.Model);

            Assert.Equal(resultProducts.Products.Count(), model.Products.Count());
            Assert.Equal(expected.BrandId, model.BrandId);
            Assert.Equal(expected.SectionId, model.SectionId);

            Assert.Equal(resultProducts.Products.First().Brand.Name, model.Products.First().Brand);
        }