예제 #1
0
        public void UpdateProductSuccess()
        {
            var existingId = Guid.NewGuid();
            var expected   = new Product {
                Id = existingId, Name = "Product New Name"
            };
            var old = new EF.Product {
                Id = existingId, Name = "Old Name"
            };

            var productRepository = new Mock <IRepository <EF.Product> >();

            productRepository.Setup(p => p.Get(It.IsAny <Guid>())).Returns(Task.FromResult(old));
            productRepository.Setup(p => p.Update(It.IsAny <EF.Product>())).Returns(Task.FromResult(expected.ToDto()));

            var categoryRepository = new Mock <IRepository <EF.Category> >();

            categoryRepository.Setup(c => c.Get(It.IsAny <Guid>())).Returns(Task.FromResult(new EF.Category {
            }));

            var logger = new Mock <ILogger <ProductService> >();

            var productService = new ProductService(productRepository.Object, categoryRepository.Object, logger.Object);

            var actual = productService.Update(expected).Result;

            Assert.NotNull(actual.Item1);
            Assert.Equal(expected.Id, actual.Item1.Id);
            Assert.NotEqual(old.Name, actual.Item1.Name);
            Assert.Equal(expected.Name, actual.Item1.Name);
        }
예제 #2
0
        public static M.Product ToDto(this EF.Product product)
        {
            if (product == null)
            {
                return(null);
            }

            return(new M.Product
            {
                Id = product.Id,
                CategoryId = product.CategoryId,
                Name = product.Name,
                Description = product.Description,
                Brand = product.Brand,
                Price = product.Price
            });
        }
예제 #3
0
        public void DeleteProductSuccess()
        {
            var expected = new EF.Product {
                Id = Guid.NewGuid(), Name = "Product Name"
            };

            var productRepository = new Mock <IRepository <EF.Product> >();

            productRepository.Setup(p => p.Get(It.IsAny <Guid>())).Returns(Task.FromResult(expected));
            productRepository.Setup(p => p.Delete(It.IsAny <Guid>())).Returns(true);
            var categoryRepository = new Mock <IRepository <EF.Category> >();

            var logger = new Mock <ILogger <ProductService> >();

            var productService = new ProductService(productRepository.Object, categoryRepository.Object, logger.Object);

            var actual = productService.Delete(expected.Id);

            Assert.True(actual);
        }