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

Couldn't instantiate proxy of class

tiagodenoronha opened this issue · comments

Hey, i'm giving the nuget package a try and encountered this exception, was it something I did on my side?

image

Can I see the DefaultOptions declaration?

It's the same as the readme:

public DbContextOptions DefaultOptions{ get; } = new DbContextOptionsBuilder().Options;

Can you try public DbContextOptions<PlantsContext> DefaultOptions{ get; } = new DbContextOptionsBuilder<PlantsContext>().Options; ?

(DbContextOptions cannot be casted to DbContextOptions<PlantsContext>)

Yep it worked! But shouldn't the object returned from the service.GetAll() (which basically returns Plant.AsEnumerable() be of type IEnumerable?

I'm getting it as an DBSet I believe..

image

You'll need to post more context I'm afraid

I can do you one better, I can give you a sample project.

Try running the test Plants_GetAll_RetunsNull on this branch: https://github.com/tiagodenoronha/PlantsIdentifier/tree/feature/testProject

When changing AsEnumerable() in the service to ToArray(), it seems to work, so I'll have to dig into the source, probably something needed for the extension method AsEnumerable() is just not mapped.

For the test Plants_GetAll_ReturnsOk, you also need to initialize the DbSet:

dbContextMock.CreateDbSetMock(x => x.Plant);

Hmm gotcha, i'll change the method to ToArray for now :)

The first test I hadn't tried it yet :b
I'll close this issue and open up a new one with "Map AsEnumerable" so you can track it better.