コード例 #1
0
        public void OpenBookDetails(string bookTitle)
        {
            var book = _catalogContext.ReferenceBooks.GetById(bookTitle);

            using var controller = new CatalogController(_bookLogic);
            _result = controller.Details(book.Id);
        }
コード例 #2
0
        public void WhenIOpenTheDetailsOfBook(string bookId)
        {
            var book = ReferenceBooks.GetById(bookId);

            var controller = new CatalogController();
            actionResult = controller.Details(book.Id);
        }
コード例 #3
0
        public void Details_Returns_NotFound_if_Product_not_Exists()
        {
            #region Arrange

            const int expected_id = 1;

            _ProductMock
            .Setup(p => p.GetProductById(It.IsAny <int>()))
            .Returns(default(ProductDTO));

            var controller = new CatalogController(_ProductMock.Object, _ConfigMock.Object);

            #endregion

            #region Act

            var result = controller.Details(expected_id, _LoggerMock.Object);

            #endregion

            #region Assert

            Assert.IsType <NotFoundResult>(result);

            #endregion
        }
コード例 #4
0
        public void OpenBookDetails(string bookId)
        {
            var book = _context.ReferenceBooks.GetById(bookId);

            using var controller = new CatalogController(_databaseContext);
            _result = controller.Details(book.Id);
        }
コード例 #5
0
        public void DetailsReturnsWithCorrectView()
        {
            // A - A - A = Arrange - Act - Assert

            #region Arrange

            const int     expectedProductId = 1;
            const decimal expectedPrice     = 10m;

            var expectedName      = $"Product id {expectedProductId}";
            var expectedBrandName = $"Brand of product { expectedProductId}";

            var productDataMock = new Mock <IProductData>();

            productDataMock
            .Setup(p => p.GetProductById(It.IsAny <int>()))
            .Returns <int>(id => new ProductDTO
            {
                Id       = id,
                Name     = $"Product id {expectedProductId}",
                ImageUrl = $"img{id}.png",
                Price    = expectedPrice,
                Brand    = new BrandDTO
                {
                    Id   = 1,
                    Name = $"Brand of product {id}"
                },
                Section = new SectionDTO
                {
                    Id   = 1,
                    Name = $"Section of product {id}"
                }
            });

            var configurationMock = new Mock <IConfiguration>();
            configurationMock.Setup(cfg => cfg[It.IsAny <string>()])
            .Returns("3");
            var controller = new CatalogController(productDataMock.Object, configurationMock.Object);

            #endregion

            #region Act

            var result = controller.Details(expectedProductId);

            #endregion

            #region Assert

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

            Assert.Equal(expectedProductId, model.Id);
            Assert.Equal(expectedName, model.Name);
            Assert.Equal(expectedPrice, model.Price);
            Assert.Equal(expectedBrandName, model.Brand);

            #endregion
        }
コード例 #6
0
        public void Details_Returns_With_Correct_View()
        {
            // A - A - A == Arrange - Act - Assert == Подготовка данных и объекта тестирования - действие над объектом - проверка утверждений

            #region Arrange

            const int     expected_product_id = 1;
            const decimal expected_price      = 10m;

            var expected_name       = $"Product id {expected_product_id}";
            var expected_brand_name = $"Brand of product {expected_product_id}";

            var product_data_mock = new Mock <IProductData>();
            product_data_mock
            .Setup(p => p.GetProductById(It.IsAny <int>()))
            .Returns <int>(id => new ProductDTO
            {
                Id       = id,
                Name     = $"Product id {id}",
                ImageUrl = $"Image_id_{id}.png",
                Order    = 1,
                Price    = expected_price,
                Brand    = new BrandDTO
                {
                    Id   = 1,
                    Name = $"Brand of product {id}"
                },
                Section = new SectionDTO
                {
                    Id   = 1,
                    Name = $"Section of product {id}"
                }
            });

            var configuration_mock = new Mock <IConfiguration>();
            configuration_mock.Setup(cfg => cfg["PageSize"]).Returns("3");

            var controller = new CatalogController(product_data_mock.Object, configuration_mock.Object);

            #endregion

            #region Act

            var result = controller.Details(expected_product_id);

            #endregion

            #region Assert

            var view_result = Assert.IsType <ViewResult>(result);
            var model       = Assert.IsAssignableFrom <ProductViewModel>(view_result.Model);

            Assert.Equal(expected_product_id, model.Id);
            Assert.Equal(expected_name, model.Name);
            Assert.Equal(expected_price, model.Price);
            Assert.Equal(expected_brand_name, model.Brand);

            #endregion
        }
コード例 #7
0
        public void Details_Returns_with_Correct_View()
        {
            #region Arrange

            // Подготовка исходных данных
            // Подготовка ожидаемых результатов
            // Подготовка объекта тестирования

            const int     expected_id    = 1;
            const decimal expected_price = 10m;

            var expected_name = $"Product id {expected_id}";

            var product_data_mock = new Mock <IProductData>();
            product_data_mock
            .Setup(p => p.GetProductById(It.IsAny <int>()))
            .Returns <int>(
                id => new ProductDTO
            {
                Id       = id,
                Name     = $"Product id {id}",
                Order    = 1,
                Price    = expected_price,
                ImageUrl = $"img_{id}.png",
                Brand    = new BrandDTO {
                    Id = 1, Name = "Brand", Order = 1
                },
                Section = new SectionDTO {
                    Id = 1, Name = "Section", Order = 1
                }
            });

            var configuration_mock = new Mock <IConfiguration>();
            configuration_mock.Setup(c => c[It.IsAny <string>()]).Returns("3");

            var controller = new CatalogController(product_data_mock.Object, configuration_mock.Object);

            #endregion

            #region Act

            // Выполнение действия
            var result = controller.Details(expected_id);

            #endregion

            #region Assert

            // Проверка утверждений

            var view_result = Assert.IsType <ViewResult>(result);
            var model       = Assert.IsAssignableFrom <ProductViewModel>(view_result.Model);

            Assert.Equal(expected_id, model.Id);
            Assert.Equal(expected_name, model.Name);
            Assert.Equal(expected_price, model.Price);

            #endregion
        }
コード例 #8
0
        public void Test_Details()
        {
            CatalogController controller = new CatalogController();

            ViewResult result = controller.Details(1) as ViewResult;

            Assert.IsNotNull(result);
        }
コード例 #9
0
        public void WhenIOpenTheDetailsOfBook(string bookId)
        {
            var book = _catalogContext.ReferenceBooks.GetById(bookId);

            var controller = new CatalogController();

            actionResult = controller.Details(book.Id);
        }
コード例 #10
0
        public void Details_Returns_With_Correct_View()
        {
            // A-A-A = Arrange - Act - Assert

            #region Arrange

            const int     expected_product_id = 1;
            const decimal expected_price      = 10m;

            var expected_name       = $"Product id {expected_product_id}";
            var expected_brand_name = $"Brand of product {expected_product_id}";

            var product_data_mock = new Mock <IProductData>();
            product_data_mock
            .Setup(p => p.GetProductById(It.IsAny <int>()))
            .Returns <int>(id => new ProductDTO
            {
                Id       = id,
                Name     = $"Product id {id}",
                ImageUrl = $"Image_id_{id}.png",
                Order    = 1,
                Price    = expected_price,
                Brand    = new BrandDTO
                {
                    Id   = 1,
                    Name = $"Brand of product {id}"
                },
                Section = new SectionDTO
                {
                    Id   = 1,
                    Name = $"Section of product {id}"
                }
            });

            var logger_mock        = new Mock <ILogger <CatalogController> >();
            var configuration_mock = new Mock <Microsoft.Extensions.Configuration.IConfiguration>();
            var controller         = new CatalogController(product_data_mock.Object, logger_mock.Object, configuration_mock.Object);

            #endregion

            #region Act

            var result = controller.Details(expected_product_id);

            #endregion

            #region Assert

            var view_result = Assert.IsType <ViewResult>(result);
            var model       = Assert.IsAssignableFrom <ProductViewModel>(view_result.Model);

            Assert.Equal(expected_product_id, model.Id);
            Assert.Equal(expected_name, model.Name);
            Assert.Equal(expected_price, model.Price);
            Assert.Equal(expected_brand_name, model.Brand);

            #endregion
        }
コード例 #11
0
        public void Details_Returns_with_Correct_View()
        {
            #region Arrange

            const decimal expected_price = 10m;
            const int     expected_id    = 1;
            const string  expected_name  = "Product 1";

            var product_data_mock = new Mock <IProductData>();
            product_data_mock
            .Setup(p => p.GetProductById(It.IsAny <int>()))
            .Returns <int>(id => new Product
            {
                Id       = id,
                Name     = $"Product {id}",
                Order    = 1,
                Price    = expected_price,
                ImageUrl = $"img_{id}.png",
                Brand    = new Brand {
                    Id = 1, Name = "Brand", Order = 1
                },
                Section = new Section {
                    Id = 1, Order = 1, Name = "Section"
                }
            });

            var configuration_mock = new Mock <IConfiguration>();
            configuration_mock
            .Setup(config => config["CatalogPageSize"])
            .Returns("6");

            var controller = new CatalogController(product_data_mock.Object, configuration_mock.Object);

            #endregion

            #region Act

            var result = controller.Details(expected_id);

            #endregion

            #region Assert

            var view_result = Assert.IsType <ViewResult>(result);
            var model       = Assert.IsAssignableFrom <ProductViewModel>(view_result.Model);

            Assert.Equal(expected_id, model.Id);
            Assert.Equal(expected_name, model.Name);
            Assert.Equal(expected_price, model.Price);

            product_data_mock.Verify(s => s.GetProductById(It.IsAny <int>()));
            product_data_mock.VerifyNoOtherCalls();

            #endregion
        }
コード例 #12
0
        public void Details_Returns_With_Correct_View()
        {
            // A-A-A = Arrange - Act - Assert
            #region Arrange - размещение данных

            const int     expected_id         = 1;
            const decimal expected_price      = 10m;
            var           expected_name       = $"Item id {expected_id}";
            var           expected_brand_name = $"Brand of item {expected_id}";

            _ProductMock
            .Setup(p => p.GetProductById(expected_id))
            .Returns <int>(id => new ProductDTO
            {
                Id       = id,
                Name     = $"Item id {id}",
                ImageUrl = $"Image_id_{id}.png",
                Order    = 0,
                Price    = expected_price,
                Brand    = new BrandDTO
                {
                    Id   = id,
                    Name = $"Brand of item {id}"
                },
                Section = new SectionDTO
                {
                    Id    = id,
                    Name  = $"Section of product {id}",
                    Order = 1
                }
            });

            var controller = new CatalogController(_ProductMock.Object, _ConfigMock.Object);


            #endregion

            #region Act - выполнение тестируемого кода

            var result = controller.Details(expected_id, _LoggerMock.Object);

            #endregion

            #region Assert - проверка утверждений

            var view_result = Assert.IsType <ViewResult>(result);
            var model       = Assert.IsAssignableFrom <ProductViewModel>(view_result.Model);

            Assert.Equal(expected_id, model.Id);
            Assert.Equal(expected_name, model.Name);
            Assert.Equal(expected_price, model.Price);
            Assert.Equal(expected_brand_name, model.Brand);

            #endregion
        }
コード例 #13
0
        public void DetailsTest(int ValidItem, int NonValidItem)
        {
            //********************************** check the valid Id **********************************

            //arrange
            var MockLibraryRepo  = new Mock <ILibraryAssetService>();
            var MockcheckoutRepo = new Mock <ICheckOutService>();
            var validId          = ValidItem;

            MockLibraryRepo.Setup(n => n.GetById(ValidItem)).Returns(GetAllLibraries()
                                                                     .FirstOrDefault(b => b.Id == ValidItem));

            MockcheckoutRepo.Setup(r => r.GetAllById(ValidItem)).Returns(GetAllCheckout()
                                                                         .Where(x => x.LibraryAsset.Id == ValidItem));

            var controller = new CatalogController(MockLibraryRepo.Object, MockcheckoutRepo.Object);

            //act
            var result = controller.Details(ValidItem);

            //assert
            var resultView  = Assert.IsType <ViewResult>(result);
            var resultModel = Assert.IsType <AssetDetailsModel>(resultView.ViewData.Model);

            Assert.Equal(ValidItem, resultModel.Id);
            Assert.Equal("C++ Step by Step", resultModel.Title);

            //********************************** check non valid Id **********************************

            //arrange
            var NonvalidId = NonValidItem;

            MockLibraryRepo.Setup(n => n.GetById(NonvalidId)).Returns(GetAllLibraries()
                                                                      .FirstOrDefault(b => b.Id == NonvalidId));

            //act
            var NonValidResult = controller.Details(NonvalidId);

            //assert
            Assert.IsType <NotFoundResult>(NonValidResult);
        }
コード例 #14
0
        public void Details_Returns_Correct_View()
        {
            #region Assert-Подготовка данных
            const int     exp_product_id    = 1;
            const decimal exp_product_price = 10m;
            var           exp_product_name  = $"Product id {exp_product_id}";
            var           exp_brand_name    = $"Brand of product {exp_product_id}";

            var mock_product_data = new Mock <IProductData>();
            mock_product_data
            .Setup(s => s.GetProductById(It.IsAny <int>()))
            .Returns <int>(id => new ProductDTO
            {
                Id       = id,
                Name     = $"Product id {id}",
                ImageUrl = $"Image_id_{id}.png",
                Order    = 1,
                Price    = exp_product_price,
                Brand    = new BrandDTO
                {
                    Id   = 1,
                    Name = $"Brand of product {id}"
                },
                Section = new SectionDTO
                {
                    Id   = 1,
                    Name = $"Section of product {id}"
                }
            });

            var configuration_mock = new Mock <IConfiguration>();
            configuration_mock.Setup(cfg => cfg["PageSize"])
            .Returns("3");


            var controller = new CatalogController(mock_product_data.Object, configuration_mock.Object);
            #endregion

            #region Act - действия
            var result = controller.Details(exp_product_id);
            #endregion

            #region Assert - проверка
            var view_result = Assert.IsType <ViewResult>(result);
            var model       = Assert.IsAssignableFrom <ProductViewModel>(view_result.Model);
            Assert.Equal(exp_product_id, model.Id);
            Assert.Equal(exp_product_name, model.Name);
            Assert.Equal(exp_product_price, model.Price);
            Assert.Equal(exp_brand_name, model.Brand);
            #endregion
        }
コード例 #15
0
        public void Details_Returns_NotFound()
        {
            var productDataMock = new Mock <IProductData>();

            productDataMock
            .Setup(p => p.GetProductById(It.IsAny <int>()))
            .Returns((Product)null);
            var configurationStub = Mock.Of <IConfiguration>();
            var controller        = new CatalogController(productDataMock.Object, configurationStub);

            var result = controller.Details(1);

            Assert.IsInstanceOfType(result, typeof(NotFoundResult));
        }
コード例 #16
0
        public void Details_Returns_With_Correct_View()
        {
            // Arrange
            const int     EXPECTED_PRODUCT_ID = 1;
            const decimal EXPECTED_PRICE      = 10m;

            var expectedName      = $"Product id {EXPECTED_PRODUCT_ID}";
            var expectedBrandName = $"Brand of product {EXPECTED_PRODUCT_ID}";

            var fakeProductData = new Mock <IProductData>();

            fakeProductData
            .Setup(p => p.GetProductById(It.IsAny <int>()))
            .Returns <int>(id => new ProductDTO
            {
                Id       = id,
                Name     = $"Product id {id}",
                ImageUrl = $"Image_id_{id}.png",
                Order    = 1,
                Price    = EXPECTED_PRICE,
                Brand    = new BrandDTO
                {
                    Id   = 1,
                    Name = $"Brand of product {id}"
                },
                Section = new SectionDTO
                {
                    Id   = 1,
                    Name = $"Section of product {id}"
                }
            });
            var configurationMock = new Mock <IConfiguration>();

            configurationMock.Setup(cfg => cfg["PageSize"]).Returns("3");

            var controller = new CatalogController(fakeProductData.Object, configurationMock.Object);

            // Act
            var result = controller.Details(EXPECTED_PRODUCT_ID);

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

            Assert.Equal(EXPECTED_PRODUCT_ID, model.Id);
            Assert.Equal(expectedName, model.Name);
            Assert.Equal(EXPECTED_PRICE, model.Price);
            Assert.Equal(expectedBrandName, model.Brand);
        }
コード例 #17
0
        public void Details_Returns_with_Correct_View()
        {
            #region Arrange

            const int     expected_product_id = 1;
            const decimal expected_price      = 10m;

            var expected_name       = $"Product id {expected_product_id}";
            var expected_brand_name = $"Brand of product {expected_product_id}";

            var product_data_mock = new Mock <IProductData>();
            product_data_mock.Setup(p => p.GetProductById(It.IsAny <int>()))
            .Returns <int>(id => new ProductDTO(
                               id,
                               $"Product id {id}",
                               1,
                               expected_price,
                               $"img{id}.png",
                               new BrandDTO(1, $"Brand of product {id}", 1, 1),
                               new SectionDTO(1, $"Section of product {id}", 1, null, 1)
                               ));

            var configuration_mock = new Mock <IConfiguration>();
            configuration_mock.Setup(configuration => configuration[It.IsAny <string>()])
            .Returns("3");

            var controller = new CatalogController(product_data_mock.Object, configuration_mock.Object);

            #endregion

            #region Act

            var result = controller.Details(expected_product_id);

            #endregion

            #region Assert

            var view_result = Assert.IsType <ViewResult>(result);
            var model       = Assert.IsAssignableFrom <ProductViewModel>(view_result.Model);

            Assert.Equal(expected_product_id, model.Id);
            Assert.Equal(expected_name, model.Name);
            Assert.Equal(expected_price, model.Price);
            Assert.Equal(expected_brand_name, model.Brand);

            #endregion
        }
コード例 #18
0
        public void DetailsReturnsCorrectView()
        {
            //arrange

            const int     expectedProductId   = 1;
            const decimal expectedPrice       = 10m;
            var           expectedProductName = "Mock Product-" + expectedProductId;
            var           expectedBrandName   = "Mock Brand For Product-" + expectedProductId;
            var           expectedImageUrl    = "Mock Image Url For Product-" + expectedProductId;
            var           expectedSectionName = "Mock Section Name For Product-" + expectedProductId;

            var productDataMock = new Mock <IProductData>();

            productDataMock.Setup(repo => repo.GetProductById(It.IsAny <int>()))
            .Returns <int>(id => new ProductDto(
                               id,
                               expectedProductName,
                               1,
                               expectedPrice,
                               expectedImageUrl,
                               new BrandDto(1, expectedBrandName, 1, 1),
                               new SectionDto(1, expectedSectionName, 1, null, 1)
                               ));

            var configurationMock = new Mock <IConfiguration>();

            configurationMock.Setup(configuration => configuration[It.IsAny <string>()]).Returns("12");

            var controller = new CatalogController(productDataMock.Object, configurationMock.Object);

            //act

            var result = controller.Details(expectedProductId);


            //assert

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

            Assert.Equal(expectedProductId, model.Id);
            Assert.Equal(expectedProductName, model.Name);
            Assert.Equal(expectedPrice, model.Price);
            Assert.Equal(expectedImageUrl, model.ImageUrl);
            Assert.Equal(expectedBrandName, model.Brand.Name);
            Assert.Equal(expectedProductName, model.Name);
        }
コード例 #19
0
        public void DetailsViewProductNotFound()
        {
            var expectedProductId = 1;

            var productDataMock = new Mock <IProductData>();

            var configMock = new Mock <IConfiguration>();

            productDataMock
            .Setup(p => p.GetProductById(It.IsAny <int>()))
            .Returns(default(ProductDTO));

            var controller = new CatalogController(productDataMock.Object, configMock.Object);

            var result = controller.Details(expectedProductId);

            var viewResult = Assert.IsType <NotFoundResult>(result);
        }
コード例 #20
0
        public void Details_Returns_NotFound_if_Product_not_Exist()
        {
            const int expected_product_id = 1;

            var product_data_mock = new Mock <IProductData>();

            product_data_mock
            .Setup(p => p.GetProductById(It.IsAny <int>()))
            .Returns(default(ProductDTO));

            var config_mock = new Mock <IConfiguration>();

            var controller = new CatalogController(product_data_mock.Object, config_mock.Object);

            var result = controller.Details(expected_product_id);

            Assert.IsType <NotFoundResult>(result);
        }
コード例 #21
0
        public void DetailsViewGetProduct()
        {
            var expectedProductId   = 1;
            var expectedPrice       = 10m;
            var expectedProductName = $"Product id {expectedProductId}";
            var expectedBrandName   = $"Brand of product {expectedProductId}";

            var productDataMock = new Mock <IProductData>();

            productDataMock
            .Setup(p => p.GetProductById(It.IsAny <int>()))
            .Returns <int>(id => new ProductDTO
            {
                Id       = id,
                Name     = $"Product id {id}",
                ImageUrl = $"Image_id_{id}.png",
                Order    = 1,
                Price    = expectedPrice,
                Brand    = new BrandDTO
                {
                    Id   = 1,
                    Name = $"Brand of product {id}"
                },
                Section = new SectionDTO
                {
                    Id   = 1,
                    Name = $"Section of product {id}"
                }
            });

            var configMock = new Mock <IConfiguration>();

            var controller = new CatalogController(productDataMock.Object, configMock.Object);

            var result = controller.Details(expectedProductId);

            var viewResult = Assert.IsType <ViewResult>(result);

            var model = Assert.IsAssignableFrom <ProductViewModel>(viewResult.Model);

            Assert.Equal(expectedProductId, model.Id);
            Assert.Equal(expectedProductName, model.Name);
            Assert.Equal(expectedBrandName, model.Brand);
        }
コード例 #22
0
        public void Details_Returns_Correct_View()
        {
            const decimal expectedPrice = 10m;
            const int     expectedId    = 1;
            const string  expectedName  = "Product 1";

            var productDataMock = new Mock <IProductData>();

            productDataMock
            .Setup(p => p.GetProductById(It.IsAny <int>()))
            .Returns <int>(id => new Product
            {
                Id       = id,
                Name     = $"Product {id}",
                Order    = 1,
                Price    = expectedPrice,
                ImageUrl = $"image_{id}.jpg",
                Brand    = new Brand {
                    Id = 1, Name = "Brand", Order = 1
                },
                Section = new Section {
                    Id = 1, Name = "Section", Order = 1
                },
            });
            var configurationStub = Mock.Of <IConfiguration>();
            var controller        = new CatalogController(productDataMock.Object, configurationStub);

            var result = controller.Details(expectedId);

            Assert.IsInstanceOfType(result, typeof(ViewResult));
            var viewResult = (ViewResult)result;

            Assert.IsInstanceOfType(viewResult.Model, typeof(ProductWebModel));
            var webModel = (ProductWebModel)viewResult.Model;

            Assert.AreEqual(expectedId, webModel.Id);
            Assert.AreEqual(expectedName, webModel.Name);
            Assert.AreEqual(expectedPrice, webModel.Price);

            productDataMock.Verify(s => s.GetProductById(It.IsAny <int>()), Times.Once);
            productDataMock.VerifyNoOtherCalls();
        }