How do I mock AsNoTracking method?
In below example, DbContext has injected to the service class.It works fine if I remove AsNoTracking extension method from GetOrderedP
Looking at the source code of the AsNoTracking() extension method:
public static IQueryable AsNoTracking(this IQueryable source)
{
var asDbQuery = source as DbQuery;
return asDbQuery != null ? asDbQuery.AsNoTracking() : CommonAsNoTracking(source);
}
Since source (your DbSet you're trying to mock) is indeed a DbQuery (because DbSet is deriving from DbQuery), it tries to invoke the 'real' (non-mocked) AsNoTracking() method which rightfully returns null.
Try to mock the AsNoTracking() method as well:
mockSet.Setup(x => x.AsNoTracking()).Returns(mockSet.Object);