romantitov / MockQueryable

Mocking Entity Framework Core operations such ToListAsync, FirstOrDefaultAsync etc
MIT License
767 stars 74 forks source link

Added support for expression when using the IEnumerable constructor. #31

Closed dlemstra closed 4 years ago

dlemstra commented 4 years ago

PR Details

This PR adds support for the following statement:

var enumerable = new TestAsyncEnumerable<string>(new[] { "a", "bb" });
var result = enumerable.Where(value => value.Length > 1).ToList();

Description

I would like to use this library to help with the mocking of DocumentDb queries. Mocking these results requires me to implement a class IDocumentQuery<T> and I would want to inherit from TestAsyncEnumerable<T> to reuse some of the code that was already written.

But calling Where on TestAsyncEnumerable<T> does not work because the Expression property is null. And this PR sets the Expression to a value that just returns the enumerable.

Checklist

dlemstra commented 4 years ago

Now wondering if the code should be split up even more. If you would remove the IAsyncQueryProvider interface and create a separate library called MockQueryable.EntityFrameworkCore the Core library would be just for mocking IAsyncEnumerable<T> IOrderedQueryable<T>. That would remove the reference toEntityFrameworkCoreand make it possible to use theCore` library in a different situation. I don't mind helping with a PR but feel free to make these changes yourself.

romantitov commented 4 years ago

@dlemstra Songs like a good idea. I'll try to find time for it this weekend

dlemstra commented 4 years ago

@romantitov I already have a proof of concept if you are interested: https://github.com/dlemstra/MockQueryable/tree/test-query-provider.

edit: Seems to not pass the tests will need to investigate :) (Found and fixed the issue)

romantitov commented 4 years ago

@dlemstra Sorry for the late answer, was busy. Thanks for the POC I've made the split between Core and EfCore based on it - you can see it in latest changes. I've also included changes from current PR, I'll close it because the PR is not relevant anymore. I'm gonna make the new version (3.1.3) of nuget packages available during the day