Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Faking IDbSet<T> with support for async operations

I am trying to unit test my first repository in a new project where we have decided to use EF6 mostly for the async stuff. I am having issues faking a IDbSet for my model, and allowing to use any Linq before using the new async niceties.

I am using a FakeDbSet as provided in this post. If I do a simple query, such as

await set.FirstOrDefaultAsync(e => e.Approved);

this works great.

The problem occurs when I try to do:

await set.OrderByDescending(e => e.Date)
         .FirstOrDefaultAsync(e => e.Approved)`

Then I get an error:

System.InvalidOperationException: The provider for the source IQueryable doesn't implement IDbAsyncQueryProvider.

By breaking apart the fluent syntax and examining the result at each step, it is clear to me that the IDbAsyncQueryProvider disappears after the first "ordinary" Linq operator (e.g. OrderBy, Where or Select).

How can I get this to work?

Update (as there was some confusion): There are 2 projects; call them Repository and Test. Test of course references Repository. The call to Where happens in Repository, and it does not know about the FakeDbSet (nor should it, since it is a test-only class).

Update 2: Simple demo project can be downloaded here. Restore Nuget packages before running unit tests.

like image 666
Vegard Larsen Avatar asked Mar 22 '13 09:03

Vegard Larsen


1 Answers

Here you have a link with the exact classes you need to implement. http://msdn.microsoft.com/en-us/data/dn314429.aspx#async

like image 81
Rikard Avatar answered Oct 02 '22 19:10

Rikard