// Data, no id match public void TestGetCityWrongId() { PointOfInterest poi1 = new PointOfInterest { Id = 1, Name = "Downtown", Description = "Lots of food" }; PointOfInterest poi2 = new PointOfInterest { Id = 2, Name = "Stanley Park", Description = "Lots to do" }; PointOfInterest poi3 = new PointOfInterest { Id = 3, Name = "Science World", Description = "Lots of science" }; PointOfInterest poi4 = new PointOfInterest { Id = 3, Name = "UBC", Description = "School" }; var cities = new List <City> { new City { Id = 1, Name = "Richmond", Description = "Small", PointsOfInterest = { poi1, poi2 } }, new City { Id = 2, Name = "Vancouver", Description = "Big", PointsOfInterest = { poi3, poi4 } }, }.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.Null(repo.GetCity(3, false)); }
// No data, thus no id match public void TestGetCityNoCities() { 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.Null(repo.GetCity(3, false)); }