Пример #1
0
        public void Delete_ThrowsArgumentException_WhenProductsAreRelatedToTheSupplier()
        {
            // Arrange
            var suppliers = new List <Supplier>
            {
                new Supplier {
                    Name = "TestSupplier1", Id = 1, CityId = 1
                },
                new Supplier {
                    Name = "TestSupplier2", Id = 2
                }
            }.AsQueryable();

            var products = new List <Product>
            {
                new Product {
                    Name = "TestProduct1", SupplierId = 1
                }
            }.AsQueryable();

            var mockDbSupplierSet = new Mock <DbSet <Supplier> >();

            mockDbSupplierSet.As <IQueryable <Supplier> >().Setup(m => m.Provider).Returns(suppliers.Provider);
            mockDbSupplierSet.As <IQueryable <Supplier> >().Setup(m => m.Expression).Returns(suppliers.Expression);
            mockDbSupplierSet.As <IQueryable <Supplier> >().Setup(m => m.ElementType).Returns(suppliers.ElementType);
            mockDbSupplierSet.As <IQueryable <Supplier> >().Setup(m => m.GetEnumerator()).Returns(suppliers.GetEnumerator());

            var mockDbProductSet = new Mock <DbSet <Product> >();

            mockDbProductSet.As <IQueryable <Product> >().Setup(m => m.Provider).Returns(products.Provider);
            mockDbProductSet.As <IQueryable <Product> >().Setup(m => m.Expression).Returns(products.Expression);
            mockDbProductSet.As <IQueryable <Product> >().Setup(m => m.ElementType).Returns(products.ElementType);
            mockDbProductSet.As <IQueryable <Product> >().Setup(m => m.GetEnumerator()).Returns(products.GetEnumerator());

            var mockContext = new Mock <EazyCartContext>();

            mockContext.Setup(c => c.Suppliers).Returns(mockDbSupplierSet.Object);
            mockContext.Setup(c => c.Products).Returns(mockDbProductSet.Object);

            var supplierBusiness = new SupplierBusiness(mockContext.Object);

            // Act & Assert
            try
            {
                int idToDelete = 1;
                supplierBusiness.Delete(idToDelete);
                Assert.Fail("No exception was thrown.");
            }
            catch (ArgumentException ex)
            {
                string expectedMessage = string.Format("One or more products are related to this supplier.");
                Assert.AreEqual(expectedMessage, ex.Message, "Wrong exception was thrown.");
            }
        }
Пример #2
0
        public void Delete_SuccessfullyDeletesSupplier_WhenNoProductsAreRelatedToTheSupplier()
        {
            // Arrange
            var suppliers = new List <Supplier>
            {
                new Supplier {
                    Name = "TestSupplier1", Id = 1, CityId = 1
                },
                new Supplier {
                    Name = "TestSupplier2", Id = 2, CityId = 1
                }
            }.AsQueryable();

            var products = new List <Product>
            {
                new Product {
                    Name = "TestProduct1", SupplierId = 1
                }
            }.AsQueryable();

            var mockDbSupplierSet = new Mock <DbSet <Supplier> >();

            mockDbSupplierSet.As <IQueryable <Supplier> >().Setup(m => m.Provider).Returns(suppliers.Provider);
            mockDbSupplierSet.As <IQueryable <Supplier> >().Setup(m => m.Expression).Returns(suppliers.Expression);
            mockDbSupplierSet.As <IQueryable <Supplier> >().Setup(m => m.ElementType).Returns(suppliers.ElementType);
            mockDbSupplierSet.As <IQueryable <Supplier> >().Setup(m => m.GetEnumerator()).Returns(suppliers.GetEnumerator());

            var mockDbProductSet = new Mock <DbSet <Product> >();

            mockDbProductSet.As <IQueryable <Product> >().Setup(m => m.Provider).Returns(products.Provider);
            mockDbProductSet.As <IQueryable <Product> >().Setup(m => m.Expression).Returns(products.Expression);
            mockDbProductSet.As <IQueryable <Product> >().Setup(m => m.ElementType).Returns(products.ElementType);
            mockDbProductSet.As <IQueryable <Product> >().Setup(m => m.GetEnumerator()).Returns(products.GetEnumerator());

            var mockContext = new Mock <EazyCartContext>();

            mockContext.Setup(c => c.Suppliers).Returns(mockDbSupplierSet.Object);
            mockContext.Setup(c => c.Products).Returns(mockDbProductSet.Object);

            var supplierBusiness = new SupplierBusiness(mockContext.Object);

            // Act
            int idToDelete = 2;

            supplierBusiness.Delete(idToDelete);

            // Assert
            mockDbSupplierSet.Verify(m => m.Remove(It.IsAny <Supplier>()), Times.Once());
            mockContext.Verify(m => m.SaveChanges(), Times.Once());
        }