I've setup the mock dbset which reads a bunch of json files and deserializes them to a mock dbset. I have added AsNoTracking to my variable for two reasons: 1) I will be storing a new object in the db and do not need to track the object as that updates the entry. 2) Performance.
The code works fine however the mocking is not working since I've added the AsNoTracking() method to not store the result in the dbcontext cache. This is returning an ArgumentNullException now. If I remove AsNoTracking the test passes however I do not want to be forced to having to remove AsNoTracking simply for the passing of unit tests.
When debugging my unit tests it throws an ArugmentNullException here:
var myObj = dbContext.MyTable.AsNoTracking().Where(b => b.Id == param.Id).Include(b => b.Column1).Include(b => b.Column2).Include(b => b.Colum3).FirstOrDefault();
However if run through a browser this works fine, data is returned correctly, the object is not null.
public Mock<DbSet<T>> GetMockDbSet<T>(string path) where T : class
{
var data = GetObjectList<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;
}
Is there anything I can do to the code above so that the result is not null if AsNoTracking() is added to var's where objects are being returned from a dbset.