public void GetAll_ReturnsAnEmptyList_WhenThereAreNoCountries() { // Arrange var countries = new List <Country>().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 var allCountries = countryBusiness.GetAll(); // Assert int expectedCount = 0; Assert.AreEqual(expectedCount, allCountries.Count, "List of all countries is not empty"); }
public void GetAll_ReturnsACorrectListOfCountries_WhenThereAreCountries() { // 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 var allCountries = countryBusiness.GetAll(); // Assert string expectedFirstCountryName = "TestCountry1"; string expectedSecondCountryName = "TestCountry2"; string expectedThirdCountryName = "TestCountry3"; Assert.AreEqual(expectedFirstCountryName, allCountries[0].Name, "First names do not match."); Assert.AreEqual(expectedSecondCountryName, allCountries[1].Name, "Second names do not match."); Assert.AreEqual(expectedThirdCountryName, allCountries[2].Name, "Third names do not match"); }