public void Admin_ShouldReturnCorrectValue()
        {
            //Arrange
            var options = TestDbContextOptionsBuilder();

            SeedTestDb(options);

            var result = (dynamic)null;

            using (var context = new P3Referential(options))
            {
                var cart = new Cart();
                var productRepository = new ProductRepository(context);
                var productService    = new ProductService(cart, productRepository, null, null);
                var languageService   = new LanguageService();
                var productController = new ProductController(productService, languageService);
                // Act
                result = productController.Admin();

                //Cleanup
                context.Database.EnsureDeleted();
            }

            //Assert
            Assert.NotNull(result);
            var viewResult = Assert.IsType <ViewResult>(result) as ViewResult;
            var model      = Assert.IsAssignableFrom <IOrderedEnumerable <ProductViewModel> >(viewResult.ViewData.Model);

            Assert.Equal(_testProductViewModels.OrderByDescending(p => p.Id), model, new ProductViewModelEqualityComparator());
        }
示例#2
0
        public void AdminTest()
        {
            var result = _productControllerRealDb.Admin();

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

            Assert.Equal(5, model.Count());
            Assert.Equal("Echo Dot", model.First(m => m.Id == 1).Name);
        }
        public void Admin_ShouldCallCorrectMethod()
        {
            //Arrange
            var sut = new ProductController(_mockProductService.Object, _mockLanguageService.Object);

            //Act
            var returnedValue = sut.Admin();

            //Assert
            _mockProductService.Verify(x => x.GetAllProductsViewModel(), Times.Once);
            Assert.IsAssignableFrom <IActionResult>(returnedValue);
            ViewResult viewResult = Assert.IsType <ViewResult>(returnedValue);
            var        model      = Assert.IsAssignableFrom <IOrderedEnumerable <ProductViewModel> >(viewResult.ViewData.Model).ToList <ProductViewModel>();

            Assert.Equal(GetMockProductViewModels().Count, model.Count);

            //Verify the data is sorted
            int i = 0;

            foreach (var p in GetMockProductViewModels().OrderByDescending(p => p.Id))
            {
                Assert.Equal(p.Id, model[i++].Id);
            }
        }