コード例 #1
0
        public void GetCountry_ReturnsACountry_WhichIsNotNull()
        {
            // Arrange
            var countries = new List <Country>
            {
                new Country {
                    Name = "TestCountry1", Id = 1
                },
                new Country {
                    Name = "TestCountry2", Id = 2
                },
                new Country {
                    Name = "TestCountry3", Id = 3
                },
            }.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 mockContext = new Mock <EazyCartContext>();

            mockContext.Setup(c => c.Countries).Returns(countryMockDbSet.Object);

            var countryBusiness = new CountryBusiness(mockContext.Object);

            // Act
            int idToGet = 2;
            var country = countryBusiness.Get(idToGet);

            // Assert
            string expectedCountryName = "TestCountry2";

            Assert.IsNotNull(country, "Country could not be extracted.");
            Assert.AreEqual(expectedCountryName, country.Name, "Names do not match.");
        }