How MockQueryable works
masterMockQueryable enables unit testing of services that depend on Entity Framework Core by allowing IQueryable<T> to execute asynchronous EF Core extensions (like ToListAsync(), FirstOrDefaultAsync(), AnyAsync(), etc.) against in-memory collections.
The Execution Pipeline
- Data Source: An in-memory collection (e.g.,
List<T>). - Mock Building: The collection is converted into a mock using
BuildMock()orBuildMockDbSet(). - Async Emulation: The library implements
IAsyncEnumerable<T>,IAsyncQueryProvider, andIAsyncEnumerator<T>to bridge the gap between standard LINQ and EF Core's async APIs. - Expression Processing: LINQ expressions are captured as expression trees.
- Expression Rewriting: An optional
ExpressionVisitorcan intercept and rewrite provider-specific expressions (likeEF.Functions.Like) into standard LINQ expressions (like.Contains()) that can run in memory. - Execution: The rewritten expression is compiled and executed against the in-memory data.