I have a moq database with methods that read the json file of serialized objects and deserialize them in moq-dbset. When I configure dbcontext, one table has all the expected results, while the other table has 0 results. My Json file is correctly formatted and verified and has 5 entries.
var airportLocationMoq = GetMockDbSet<Repository.Location>(@"Files/ObjectJson/Airports.json");
var storageAreaMoq = GetMockDbSet<Repository.StorageArea>(@"Files/ObjectJson/StorageAreas.json");
var dbContext = new Mock<DbContext>();
dbContext.Setup(x => x.Locations).Returns(airportLocationMoq.Object);
dbContext.Setup(x => x.StorageAreas).Returns(storageAreaMoq.Object);
var airportsFromDb = dbContext.Object.Locations.Where(x => x.Type == 1).ToList();
var storageAreasFromDb = dbContext.Object.StorageAreas.ToList();
These are the methods that read the json file and deserialize them in moq-dbset:
public string LoadFile(string path)
{
return File.ReadAllText(path);
}
public List<T> GetData<T>(string path)
{
string json = LoadFile(path);
var dataList = JsonConvert.DeserializeObject<List<T>>(json);
return dataList;
}
public Mock<DbSet<T>> GetMockDbSet<T>(string path) where T : class
{
var data = GetData<T>(path).AsQueryable();
var mockSet = new Mock<DbSet<T>>();
mockSet.As<IQueryable<T>>().Setup(m => m.Provider).Returns(data.Provider);
mockSet.As<IQueryable<T>>().Setup(m => m.Expression).Returns(data.Expression);
mockSet.As<IQueryable<T>>().Setup(m => m.ElementType).Returns(data.ElementType);
mockSet.As<IQueryable<T>>().Setup(m => m.GetEnumerator()).Returns(data.GetEnumerator());
return mockSet;
}
return mockSet , , , , , , " ". , . , , , . .


user5524649