huysentruitw / entity-framework-core-mock

Easy Mock wrapper for mocking EFCore5 DbContext and DbSet using Moq or NSubstitute

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

DbSet AsQueryable() returns empty list instead of data and crash on ToListAsync()

codingyourlife opened this issue · comments

In the attached screenshots you can see that the result of my ServiceSet table contains data but calling .AsQueryable() on it does not return any data so the variable is an empty list (see other image).

Furthermore my UnitTest crashes in this function if I use ToListAsync(). If I use ToList() it works but I just realized that the test doesn't seem to work right then.

Is this a bug or can this be configured/mocked somehow?

Here you can see there is mocked data in the table:
Screenshot 2021-06-24 at 22 29 22

And it gets lost when calling .AsQueryable() on it:
Screenshot 2021-06-24 at 22 29 36

Can you provide a unit-test or minimal reproducible example that demonstrates the issue?

Sure. Was also interested in looking a bit deeper and created a Fork and a Pullrequest:
https://github.com/cup-of-tea-dot-be/entity-framework-core-mock/pull/37/files

Sadly nothing you can blindly merge but I did my best.

I meanwhile also needed the .Entry(myDbset) function so I reference issue #34 which I also created a basic unit test for that fails.

I think I could successfully fix the .AsQueryable() function but I'm not experienced in Moq enough to mock the .Entry() method. I hope you can figure it out and that I can learn from your addition :)

@huysentruitw friendly ping :). Will you have time for this any time soon?

FINALLY figured out at least a workaround. Instead of using .AsQueryable() you can write .Include(x => x). Looks like it does nothing but it ensures that you have an IQueryable instead of an IEnumerable.