Esempio n. 1
0
        public void Update_ThrowsArgumentException_WhenCountryDoesNotExist()
        {
            // 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 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 mockContext = new Mock <EazyCartContext>();

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

            var cityBusiness = new CityBusiness(mockContext.Object);

            // Act & Assert
            try
            {
                cityBusiness.Update("UpdatedCity1", 1, "InvalidCountry");
                Assert.Fail("No exception was thrown");
            }
            catch (ArgumentException ex)
            {
                string expectedMessage = string.Format("No such country exists.");
                Assert.AreEqual(expectedMessage, ex.Message, "Wrong exception was thrown.");
            }
        }
Esempio n. 2
0
        public void Update_SuccessfullyUpdatesCity_WhenCityIdIsNotDuplicate()
        {
            // 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
                },
                new City {
                    Name = "TestCity2", Id = 2, CountryId = 2
                }
            }.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 citymockDbSet = new Mock <DbSet <City> >();

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

            var mockContext = new Mock <EazyCartContext>();

            mockContext.Setup(c => c.Cities).Returns(citymockDbSet.Object);
            mockContext.Setup(c => c.Countries).Returns(mockDbCountrySet.Object);

            var cityBusiness = new CityBusiness(mockContext.Object);

            // Act
            cityBusiness.Update("UpdatedCity", 1, "TestCountry1");

            // Assert
            mockContext.Verify(m => m.SaveChanges(), Times.Once());
        }