huysentruitw / entity-framework-core-mock

Easy Mock wrapper for mocking EFCore5 DbContext and DbSet using Moq or NSubstitute
MIT License
132 stars 24 forks source link

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

Closed codingyourlife closed 2 years ago

codingyourlife commented 3 years ago

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
huysentruitw commented 3 years ago

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

codingyourlife commented 3 years ago

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 https://github.com/cup-of-tea-dot-be/entity-framework-core-mock/issues/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 :)

codingyourlife commented 3 years ago

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

codingyourlife commented 2 years ago

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.