0

I'm trying to mock some DB access code using simple lists. It generally works except for Async. which causes the error saying the provider does not support Iqueriable. I tries a few suggested solutions but they seem to cause an expression error when executing the linq query

[Fact]
public async Task TestAsyncFromList()
{
    
    List<int> items = new List<int>() { 1,2,3,4,5,6,7,8,9,10};
    var queryable = items.AsQueryable();
    var totalSync = queryable.Count();
    var totalAsync = await queryable.CountAsync(); // The provider for the source 'IQueryable' doesn't implement 'IAsyncQueryProvider'
    Assert.True(totalSync==totalAsync);
}

I was expecting that there is an 'AsAsyncQueiable' solution Tried this The provider for the source IQueryable doesn't implement IAsyncQueryProvider - but got Expression error

I was after a general solution - not something that uses EF in memory or SQLlite In memory ( Although these do work for testing ). It turns out that the actual solution was to use MockQueryable.Moq; BUT that wont work for value types - they must be Ref types

  • What is your end goal? Are you trying to test EF6 code or EFCore code or something else? – Igor Feb 18 '23 at 21:04
  • As the message says [...]Async only works if the underlying provider supports it. List does not. And there is really no need to us async here. – marsze Feb 18 '23 at 23:26
  • Yes @igor- I was originally trying to mock EFCORE for testing - I have a workaround by using SQL lite in memory - but I'd still like to understand how to solve this problem - it just seems simple. – Ewen Stewart Feb 19 '23 at 20:20
  • @marsze I get its not supported but ideally I would have hoped the Asynchronous methods would use synchronous ones as a fall back. The code is an example to show the problem - the original purpose was testing - its not actually being used like the above case – Ewen Stewart Feb 19 '23 at 20:20
  • See the marked duplicate. The answer illustrates how to mock your DbContext and DbSet instances for EFCore which uses an InMemory instance of the DbContext. – Igor Feb 20 '23 at 18:50
  • Thanks @Igor - I do already use SQLLITE in memory - but for various reasons I also need plain old objects that can be saved to JSON eg something that is NOT EF core. Anyway I did find a solution in the article you referred to - which is to use MockQueryable.Moq [https://stackoverflow.com/questions/71994567/how-to-moq-setup-for-await-with-tolistasync-variable-which-is-already-queried-as] - the only problem is that it wont work for value types (eg like a list of ints as above ) - This is ok am able to use a list of ref types - so thanks for pointing me in the right direction – Ewen Stewart Feb 22 '23 at 21:31

0 Answers0