// Cities exist thus list of cities public void TestGetCities() { var cities = new List <City> { new City { Id = 1, Name = "Richmond", Description = "Small" }, new City { Id = 2, Name = "Vancouver", Description = "Big" } }.AsQueryable(); var mockSet = new Mock <DbSet <City> >(); mockSet.As <IQueryable <City> >().Setup(m => m.Provider).Returns(cities.Provider); mockSet.As <IQueryable <City> >().Setup(m => m.Expression).Returns(cities.Expression); mockSet.As <IQueryable <City> >().Setup(m => m.ElementType).Returns(cities.ElementType); var mockContext = new Mock <CityInfoContext>(); mockContext.Setup(m => m.Cities).Returns(mockSet.Object); var repo = new CityInfoRepository(mockContext.Object); var cities2 = repo.GetCities(); Assert.Equal(2, cities2.Count()); Assert.Equal(1, cities2.First().Id); Assert.Equal("Richmond", cities2.First().Name); Assert.Equal("Small", cities2.First().Description); Assert.Equal(2, cities2.Last().Id); Assert.Equal("Vancouver", cities2.Last().Name); Assert.Equal("Big", cities2.Last().Description); }
// No cities thus empty list public void TestGetCitiesNoCities() { var cities = new List <City> { }.AsQueryable(); var mockSet = new Mock <DbSet <City> >(); mockSet.As <IQueryable <City> >().Setup(m => m.Provider).Returns(cities.Provider); mockSet.As <IQueryable <City> >().Setup(m => m.Expression).Returns(cities.Expression); mockSet.As <IQueryable <City> >().Setup(m => m.ElementType).Returns(cities.ElementType); var mockContext = new Mock <CityInfoContext>(); mockContext.Setup(m => m.Cities).Returns(mockSet.Object); var repo = new CityInfoRepository(mockContext.Object); Assert.Empty(repo.GetCities()); }