I installed mock dbset, which reads a bunch of json files and deserializes them into mock dbset. I added AsNoTracking to my variable for two reasons: 1) I will store the new object in db and do not need to track the object, as it updates the record. 2) Performance.
The code works fine, but the bullying does not work, since I added the AsNoTracking () method so as not to save the result in the dbcontext cache. Now this returns an ArgumentNullException. If I remove AsNoTracking, test passes, however, I do not want to be forced to remove AsNoTracking just to pass 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 you launch the browser, this works fine, the data will return 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 with the code above so that the result is not null if AsnoTracking () is added to var where the objects are returned from dbset.
user5524649
source
share