Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EF Core 3.1: New Overrides #25

Closed
JakenVeina opened this issue Jan 12, 2020 · 6 comments
Closed

EF Core 3.1: New Overrides #25

JakenVeina opened this issue Jan 12, 2020 · 6 comments
Assignees

Comments

@JakenVeina
Copy link

Protip: When supporting EF Core 3.1, mocked DbSet<> objects need to additionally override the newly-added methods .AsQueryable() and .AsAsyncEnumerable() (which simply return this;) within BuildMockDbSet() calls.

Anyone who (like me) was using the .AsQueryable() extension method upon a DbSet<> in production code will, after upgrade to 3.1, be calling the methods upon DbSet<> instead. In my case, this meant that instead of getting a cast of the DbSet<> to IQueryable<> I was getting an auto-generated default implementation from Moq's DefaultValueProvider, which is basically Array.Empty<T>.AsQueryable().

@romantitov
Copy link
Owner

@JakenVeina thank you for the feedback.
Not sure that the issue is clear for me.
I've tried to run tests in the solution with EF Core 3.1.1, and all tests is green. Could you provide an example of failed test to reproduce the issue?

@JakenVeina
Copy link
Author

Basically, I was doing

var query = _context.Set<MyEntity>()
    .AsQueryable();

if(...)
    query = query.Where(...);

in a method under test. The .AsQueryable() call was just my way of casting to IQueryable<T> so I can re-use the variable query.

The change is that in 3.0, .AsQueryable() resolves to the LINQ extension method, while in 3.1, it resolves to the new method that was added to DbSet<>. So, when I've configured the test to have _context.Set<MyEntity>() return a mocked DbSet<> the behavior suddenly changed. It took me a couple hours to figure out why the set was coming back empty, and it was that Mock<DbSet<>> specifically has logic to return something other than null for methods returning IQueryable<T> It just builds a queryable out of an empty array.

Easily worked around on my end, once I realized what the issue is, but this seems like a good candidate to be added to the .Setup() calls that MockQueryable does for you.

@romantitov
Copy link
Owner

The issue fixed in release 3.1.1

@likemike91
Copy link

Is that also fixed for NSubstitute?
Unfortunately I get the same error when using NSubstitute and I didn't see the fix in the code of the NSubstitute extension?

@romantitov
Copy link
Owner

@likemike91 could you open a new issue if you still facing with the error

@krilllind
Copy link
Contributor

@likemike91 Created a new ticket and PR. Hopefully we can get this fixed 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants