Ejemplo n.º 1
0
        public void Add_SuccessfullyAddsSupplier_WhenIdIsNotDuplicate()
        {
            // Arrange
            var countries = new List <Country>
            {
                new Country {
                    Name = "TestCountry1", Id = 1
                }
            }.AsQueryable();

            var cities = new List <City>
            {
                new City {
                    Name = "TestCity1", Id = 1, CountryId = 1
                }
            }.AsQueryable();

            var supplierData = new List <Supplier>().AsQueryable();

            var mockDbCountrySet = new Mock <DbSet <Country> >();

            mockDbCountrySet.As <IQueryable <Country> >().Setup(m => m.Provider).Returns(countries.Provider);
            mockDbCountrySet.As <IQueryable <Country> >().Setup(m => m.Expression).Returns(countries.Expression);
            mockDbCountrySet.As <IQueryable <Country> >().Setup(m => m.ElementType).Returns(countries.ElementType);
            mockDbCountrySet.As <IQueryable <Country> >().Setup(m => m.GetEnumerator()).Returns(countries.GetEnumerator());

            var mockDbCitySet = new Mock <DbSet <City> >();

            mockDbCitySet.As <IQueryable <City> >().Setup(m => m.Provider).Returns(cities.Provider);
            mockDbCitySet.As <IQueryable <City> >().Setup(m => m.Expression).Returns(cities.Expression);
            mockDbCitySet.As <IQueryable <City> >().Setup(m => m.ElementType).Returns(cities.ElementType);
            mockDbCitySet.As <IQueryable <City> >().Setup(m => m.GetEnumerator()).Returns(cities.GetEnumerator());

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

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

            var mockContext = new Mock <EazyCartContext>();

            mockContext.Setup(c => c.Cities).Returns(mockDbCitySet.Object);
            mockContext.Setup(c => c.Countries).Returns(mockDbCountrySet.Object);
            mockContext.Setup(c => c.Suppliers).Returns(mockDbSupplierSet.Object);

            var supplierBusiness = new SupplierBusiness(mockContext.Object);

            // Act
            supplierBusiness.Add("TestSupplier1", 1, "TestCity1", "TestCountry1");

            // Assert
            mockDbSupplierSet.Verify(m => m.Add(It.IsAny <Supplier>()), Times.Once());
            mockContext.Verify(m => m.SaveChanges(), Times.Once());
        }
Ejemplo n.º 2
0
        public void Add_ThrowsArgumentException_WhenSupplierWithIdAlreadyExists()
        {
            // Arrange
            var countries = new List <Country>
            {
                new Country {
                    Name = "TestCountry1", Id = 1
                }
            }.AsQueryable();

            var cities = new List <City>
            {
                new City {
                    Name = "TestCity1", Id = 1, CountryId = 1
                }
            }.AsQueryable();

            var suppliers = new List <Supplier>
            {
                new Supplier {
                    Name = "TestSupplier1", Id = 1, CityId = 1
                }
            }.AsQueryable();

            var mockDbCountrySet = new Mock <DbSet <Country> >();

            mockDbCountrySet.As <IQueryable <Country> >().Setup(m => m.Provider).Returns(countries.Provider);
            mockDbCountrySet.As <IQueryable <Country> >().Setup(m => m.Expression).Returns(countries.Expression);
            mockDbCountrySet.As <IQueryable <Country> >().Setup(m => m.ElementType).Returns(countries.ElementType);
            mockDbCountrySet.As <IQueryable <Country> >().Setup(m => m.GetEnumerator()).Returns(countries.GetEnumerator());

            var mockDbCitySet = new Mock <DbSet <City> >();

            mockDbCitySet.As <IQueryable <City> >().Setup(m => m.Provider).Returns(cities.Provider);
            mockDbCitySet.As <IQueryable <City> >().Setup(m => m.Expression).Returns(cities.Expression);
            mockDbCitySet.As <IQueryable <City> >().Setup(m => m.ElementType).Returns(cities.ElementType);
            mockDbCitySet.As <IQueryable <City> >().Setup(m => m.GetEnumerator()).Returns(cities.GetEnumerator());

            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 mockContext = new Mock <EazyCartContext>();

            mockContext.Setup(c => c.Cities).Returns(mockDbCitySet.Object);
            mockContext.Setup(c => c.Countries).Returns(mockDbCountrySet.Object);
            mockContext.Setup(c => c.Suppliers).Returns(mockDbSupplierSet.Object);
            mockContext.Setup(c => c.SaveChanges()).Throws(new Exception());

            var supplierBusiness = new SupplierBusiness(mockContext.Object);
            int duplicateId      = 1;

            // Act & Assert
            try
            {
                supplierBusiness.Add("TestSupplier1", 1, "TestCity1", "TestCountry1");
                supplierBusiness.Add("TestSupplier2", 1, "TestCity1", "TestCountry1");
                Assert.Fail("No exception was thrown");
            }
            catch (ArgumentException ex)
            {
                string expectedMessage = string.Format($"Supplier with ID {duplicateId} already exists.");
                Assert.AreEqual(expectedMessage, ex.Message, "Wrong exception was thrown.");
            }
        }
Ejemplo n.º 3
0
        public void Add_ThrowsArgumentException_WhenCountryIdOrCityDoesNotExist()
        {
            // Arrange
            var countries = new List <Country>
            {
                new Country {
                    Name = "TestCountry1", Id = 1
                }
            }.AsQueryable();

            var cities = new List <City>
            {
                new City {
                    Name = "TestCity1", Id = 1, CountryId = 1
                }
            }.AsQueryable();

            var suppliers = new List <Supplier>
            {
                new Supplier {
                    Name = "TestSupplier1", Id = 1, CityId = 1
                }
            }.AsQueryable();

            var mockDbCountrySet = new Mock <DbSet <Country> >();

            mockDbCountrySet.As <IQueryable <Country> >().Setup(m => m.Provider).Returns(countries.Provider);
            mockDbCountrySet.As <IQueryable <Country> >().Setup(m => m.Expression).Returns(countries.Expression);
            mockDbCountrySet.As <IQueryable <Country> >().Setup(m => m.ElementType).Returns(countries.ElementType);
            mockDbCountrySet.As <IQueryable <Country> >().Setup(m => m.GetEnumerator()).Returns(countries.GetEnumerator());

            var mockDbCitySet = new Mock <DbSet <City> >();

            mockDbCitySet.As <IQueryable <City> >().Setup(m => m.Provider).Returns(cities.Provider);
            mockDbCitySet.As <IQueryable <City> >().Setup(m => m.Expression).Returns(cities.Expression);
            mockDbCitySet.As <IQueryable <City> >().Setup(m => m.ElementType).Returns(cities.ElementType);
            mockDbCitySet.As <IQueryable <City> >().Setup(m => m.GetEnumerator()).Returns(cities.GetEnumerator());

            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 mockContext = new Mock <EazyCartContext>();

            mockContext.Setup(c => c.Cities).Returns(mockDbCitySet.Object);
            mockContext.Setup(c => c.Countries).Returns(mockDbCountrySet.Object);
            mockContext.Setup(c => c.Suppliers).Returns(mockDbSupplierSet.Object);

            var supplierBusiness = new SupplierBusiness(mockContext.Object);

            // Act & Assert
            try
            {
                supplierBusiness.Add("TestSupplier1", 1, "InvalidCity", "InvalidCountry");
                Assert.Fail("No exception was thrown");
            }
            catch (ArgumentException ex)
            {
                string expectedMessage = string.Format("No such country/city exists");
                Assert.AreEqual(expectedMessage, ex.Message, "Wrong exception was thrown.");
            }
        }