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

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

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

            countryMockDbSet.As <IQueryable <Country> >().Setup(m => m.Provider).Returns(countries.Provider);
            countryMockDbSet.As <IQueryable <Country> >().Setup(m => m.Expression).Returns(countries.Expression);
            countryMockDbSet.As <IQueryable <Country> >().Setup(m => m.ElementType).Returns(countries.ElementType);
            countryMockDbSet.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.Countries).Returns(countryMockDbSet.Object);
            mockContext.Setup(c => c.Cities).Returns(cityMockDbSet.Object);

            var countryBusiness = new CountryBusiness(mockContext.Object);

            // Act
            try
            {
                int idToDelete = 1;
                countryBusiness.Delete(idToDelete);
                Assert.Fail("No exception was thrown.");
            }
            catch (ArgumentException exc)
            {
                string expectedMessage = "One or more cities are related to this country.";
                Assert.AreEqual(expectedMessage, exc.Message, "Another exception was thrown.");
            }
        }
        public void Delete_DeletesCountriesSuccessfully_WhenNoCitiesAreRelatedToCountry()
        {
            // Arrange
            var countries = new List <Country>
            {
                new Country {
                    Name = "TestCountry1", Id = 1
                },
                new Country {
                    Name = "TestCountry2", Id = 2
                },
            }.AsQueryable();

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

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

            countryMockDbSet.As <IQueryable <Country> >().Setup(m => m.Provider).Returns(countries.Provider);
            countryMockDbSet.As <IQueryable <Country> >().Setup(m => m.Expression).Returns(countries.Expression);
            countryMockDbSet.As <IQueryable <Country> >().Setup(m => m.ElementType).Returns(countries.ElementType);
            countryMockDbSet.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.Countries).Returns(countryMockDbSet.Object);
            mockContext.Setup(c => c.Cities).Returns(cityMockDbSet.Object);

            var countryBusiness = new CountryBusiness(mockContext.Object);

            // Act
            int idToDelete = 2;

            countryBusiness.Delete(idToDelete);

            // Assert
            countryMockDbSet.Verify(x => x.Remove(It.IsAny <Country>()), Times.Once);
            mockContext.Verify(x => x.SaveChanges(), Times.Once);
        }
Beispiel #3
0
 public void Delete(int id)
 {
     business.Delete(id);
 }