Exemplo n.º 1
0
        public void TestGetProducts_ShouldReturnCorrectCollection()
        {
            var firstMockedProduct  = new Mock <Product>();
            var secondMockedProduct = new Mock <Product>();
            var thirdMockedProduct  = new Mock <Product>();

            var collection = new List <Product>
            {
                firstMockedProduct.Object,
                secondMockedProduct.Object,
                thirdMockedProduct.Object
            };

            var factory    = new Mock <IProductFactory>();
            var repository = new Mock <IRepository <Product> >();

            repository.Setup(x => x.Entities).Returns(collection);

            var categoryService = new Mock <ICategoryService>();
            var unitOfWork      = new Mock <IUnitOfWork>();

            var service = new ZobShop.Services.ProductService(factory.Object, repository.Object, categoryService.Object, unitOfWork.Object);

            var result = service.GetProducts();

            Assert.AreSame(collection, result);
        }
        public void TestGetProductsByCategory_ShouldReturnCorrectly(string category)
        {
            var firstMockedProduct  = new Mock <Product>();
            var secondMockedProduct = new Mock <Product>();
            var thirdMockedProduct  = new Mock <Product>();

            var collection = new List <Product>
            {
                firstMockedProduct.Object,
                secondMockedProduct.Object,
                thirdMockedProduct.Object
            };

            var factory    = new Mock <IProductFactory>();
            var repository = new Mock <IRepository <Product> >();

            repository.Setup(x => x.GetAll(It.IsAny <Expression <Func <Product, bool> > >())).Returns(collection);

            var categoryService = new Mock <ICategoryService>();
            var unitOfWork      = new Mock <IUnitOfWork>();

            var service = new ZobShop.Services.ProductService(factory.Object, repository.Object, categoryService.Object, unitOfWork.Object);

            var result = service.GetProductsByCategory(category);

            Assert.AreEqual(collection, result);
        }
Exemplo n.º 3
0
        public void TestGetProducts_ShouldCallRepositoryEntities()
        {
            var factory         = new Mock <IProductFactory>();
            var repository      = new Mock <IRepository <Product> >();
            var categoryService = new Mock <ICategoryService>();
            var unitOfWork      = new Mock <IUnitOfWork>();

            var service = new ZobShop.Services.ProductService(factory.Object, repository.Object, categoryService.Object, unitOfWork.Object);

            service.GetProducts();

            repository.Verify(x => x.Entities, Times.Once);
        }
        public void TestGetProductsByCategory_ShouldCallRepositoryGetAll(string category)
        {
            var factory         = new Mock <IProductFactory>();
            var repository      = new Mock <IRepository <Product> >();
            var categoryService = new Mock <ICategoryService>();
            var unitOfWork      = new Mock <IUnitOfWork>();

            var service = new ZobShop.Services.ProductService(factory.Object, repository.Object, categoryService.Object, unitOfWork.Object);

            service.GetProductsByCategory(category);

            repository.Verify(x => x.GetAll(It.IsAny <Expression <Func <Product, bool> > >()), Times.Once);
        }
Exemplo n.º 5
0
        public void TestDeleteProduct_ShouldCallRepositoryGetByIdCorrectly(int id)
        {
            var factory         = new Mock <IProductFactory>();
            var repository      = new Mock <IRepository <Product> >();
            var categoryService = new Mock <ICategoryService>();
            var unitOfWork      = new Mock <IUnitOfWork>();

            var service = new ZobShop.Services.ProductService(factory.Object, repository.Object, categoryService.Object, unitOfWork.Object);

            service.DeleteProduct(id);

            repository.Verify(x => x.GetById(id), Times.Once);
        }
Exemplo n.º 6
0
        public void TestEditProduct_ShouldCallUnitOfWorkCommitCorrectly()
        {
            var factory         = new Mock <IProductFactory>();
            var repository      = new Mock <IRepository <Product> >();
            var categoryService = new Mock <ICategoryService>();
            var unitOfWork      = new Mock <IUnitOfWork>();

            var service = new ZobShop.Services.ProductService(factory.Object, repository.Object, categoryService.Object, unitOfWork.Object);

            var mockedProduct = new Mock <Product>();

            service.EditProduct(mockedProduct.Object);

            unitOfWork.Verify(x => x.Commit(), Times.Once);
        }
Exemplo n.º 7
0
        public void TestDeleteProduct_NoProductFound_ShouldNotCallUnitOfWorkCommit(int id)
        {
            var factory    = new Mock <IProductFactory>();
            var repository = new Mock <IRepository <Product> >();

            repository.Setup(x => x.GetById(It.IsAny <object>()))
            .Returns((Product)null);

            var categoryService = new Mock <ICategoryService>();
            var unitOfWork      = new Mock <IUnitOfWork>();

            var service = new ZobShop.Services.ProductService(factory.Object, repository.Object, categoryService.Object, unitOfWork.Object);

            service.DeleteProduct(id);

            unitOfWork.Verify(x => x.Commit(), Times.Never);
        }
Exemplo n.º 8
0
        public void TestGetById_ShouldReturnCorrectly(int id)
        {
            var mockedProduct = new Mock <Product>();

            var factory    = new Mock <IProductFactory>();
            var repository = new Mock <IRepository <Product> >();

            repository.Setup(x => x.GetById(It.IsAny <object>())).Returns(mockedProduct.Object);

            var categoryService = new Mock <ICategoryService>();
            var unitOfWork      = new Mock <IUnitOfWork>();

            var service = new ZobShop.Services.ProductService(factory.Object, repository.Object, categoryService.Object, unitOfWork.Object);

            var result = service.GetById(id);

            Assert.AreSame(mockedProduct.Object, result);
        }
Exemplo n.º 9
0
        public void TestDeleteProduct_ShouldCallUnitOfWorkCommitCorrectly(int id)
        {
            var factory = new Mock <IProductFactory>();

            var mockedProduct = new Mock <Product>();

            var repository = new Mock <IRepository <Product> >();

            repository.Setup(x => x.GetById(It.IsAny <object>()))
            .Returns(mockedProduct.Object);

            var categoryService = new Mock <ICategoryService>();
            var unitOfWork      = new Mock <IUnitOfWork>();

            var service = new ZobShop.Services.ProductService(factory.Object, repository.Object, categoryService.Object, unitOfWork.Object);

            service.DeleteProduct(id);

            unitOfWork.Verify(x => x.Commit(), Times.Once);
        }
Exemplo n.º 10
0
        public void TestCreateProduct_ShouldCallRepositoryAdd(string name,
                                                              string categoryName,
                                                              int quantity,
                                                              decimal price,
                                                              double volume,
                                                              string maker,
                                                              string imageMimeType)
        {
            var mockedProduct = new Mock <Product>();

            var factory = new Mock <IProductFactory>();

            factory.Setup(f => f.CreateProduct(It.IsAny <string>(),
                                               It.IsAny <Category>(),
                                               It.IsAny <int>(),
                                               It.IsAny <decimal>(),
                                               It.IsAny <double>(),
                                               It.IsAny <string>(),
                                               It.IsAny <string>(),
                                               It.IsAny <byte[]>()))
            .Returns(mockedProduct.Object);

            var repository      = new Mock <IRepository <Product> >();
            var categoryService = new Mock <ICategoryService>();

            categoryService.Setup(x => x.GetCategoryByName(It.IsAny <string>())).Returns(It.IsAny <Category>());

            var unitOfWork = new Mock <IUnitOfWork>();

            var service = new ZobShop.Services.ProductService(factory.Object, repository.Object, categoryService.Object, unitOfWork.Object);

            var buffer = new byte[2];

            service.CreateProduct(name, categoryName, quantity, price, volume, maker, imageMimeType, buffer);

            repository.Verify(x => x.Add(mockedProduct.Object), Times.Once);
        }
Exemplo n.º 11
0
        public void TestCreateProduct_ServiceReturnsNull_ShouldCallCategoryServiceCreateCategoryWithCorrectValue(string name,
                                                                                                                 string categoryName,
                                                                                                                 int quantity,
                                                                                                                 decimal price,
                                                                                                                 double volume,
                                                                                                                 string maker,
                                                                                                                 string imageMimeType)
        {
            var factory         = new Mock <IProductFactory>();
            var repository      = new Mock <IRepository <Product> >();
            var categoryService = new Mock <ICategoryService>();

            categoryService.Setup(x => x.GetCategoryByName(It.IsAny <string>())).Returns(It.IsAny <Category>());

            var unitOfWork = new Mock <IUnitOfWork>();

            var service = new ZobShop.Services.ProductService(factory.Object, repository.Object, categoryService.Object, unitOfWork.Object);

            var buffer = new byte[3];

            service.CreateProduct(name, categoryName, quantity, price, volume, maker, imageMimeType, buffer);

            categoryService.Verify(x => x.CreateCategory(categoryName), Times.Once);
        }