public void GetAllNames_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 allCountryNames = countryBusiness.GetAllNames(); // Assert int expectedCount = 0; Assert.AreEqual(expectedCount, allCountryNames.Count, "List of all country names is not empty"); }
public void GetAllNames_ReturnsACorrectListOfCountryNames_WhenNotEmpty() { // 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 allCountryNames = countryBusiness.GetAllNames(); // Assert string expectedFirstCountryName = "TestCountry1"; string expectedSecondCountryName = "TestCountry2"; string expectedThirdCountryName = "TestCountry3"; Assert.AreEqual(expectedFirstCountryName, allCountryNames[0], "First names do not match."); Assert.AreEqual(expectedSecondCountryName, allCountryNames[1], "Second names do not match."); Assert.AreEqual(expectedThirdCountryName, allCountryNames[2], "Third names do not match"); }