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

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

            var suppliers = new List <Supplier>()
            {
                new Supplier()
                {
                    Id = 1, Name = "TestSupplier1", CityId = 1
                },
                new Supplier()
                {
                    Id = 2, Name = "TestSupplier2", CityId = 2
                },
            }.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 supplierMockDbSet = new Mock <DbSet <Supplier> >();

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

            var mockContext = new Mock <EazyCartContext>();

            mockContext.Setup(c => c.Countries).Returns(countryMockDbSet.Object);
            mockContext.Setup(c => c.Cities).Returns(cityMockDbSet.Object);
            mockContext.Setup(c => c.Suppliers).Returns(supplierMockDbSet.Object);

            var countryBusiness = new CountryBusiness(mockContext.Object);

            // Act
            string supplierName      = "TestSupplier1";
            string actualCountryName = countryBusiness.GetNameBySupplier(supplierName);

            // Assert
            string expectedCountryName = "TestCountry1";

            Assert.AreEqual(expectedCountryName, actualCountryName, "Country names do not match.");
        }