rgvlee / EntityFrameworkCore.Testing

Adds relational support to the Microsoft EntityFrameworkCore in-memory database provider by mocking relational operations.
MIT License
157 stars 15 forks source link

Unable to create dbcontext mock #112

Open click2install opened 2 years ago

click2install commented 2 years ago

Using efcore 5.0.9. Using any of the 3.x.y versions of your package yield the same result whether using the Create.MockedDbContextFor<MyDbContext>(); or

var options = new DbContextOptionsBuilder<MyDbContext>()
                .UseInMemoryDatabase(Guid.NewGuid().ToString())
                .Options;
var dbContextToMock = new MyDbContext(options);
var context = new MockedDbContextBuilder<MyDbContext>()
                .UseDbContext(dbContextToMock)
                .UseConstructorWithParameters(options)
                .MockedDbContext;

The code will throw NSubstitute.Exceptions.CouldNotSetReturnDueToNoLastCallException: 'Could not find a call to return from. with stack trace

   at NSubstitute.Core.ThreadLocalContext.LastCallShouldReturn(IReturn value, MatchArgs matchArgs)
   at NSubstitute.SubstituteExtensions.ConfigureReturn[T](MatchArgs matchArgs, Func`2 returnThis, Func`2[] returnThese)
   at EntityFrameworkCore.Testing.NSubstitute.Helpers.MockedDbContextFactory`1.Create()
   at EntityFrameworkCore.Testing.NSubstitute.Helpers.MockedDbContextBuilder`1.get_MockedDbContext()

I have a public ctor on the dbcontext as

public MyDbContext(DbContextOptions<MyDbContext> options)
            : base(options)
        {
        }
rgvlee commented 2 years ago

Hi @click2install, thanks for raising an issue. Can you provide a minimum reproducible example please.

click2install commented 2 years ago

Thanks, @rgvlee we were in a pinch and went with another option. I will attempt to get a repro done though as this API looks more powerful.

rgvlee commented 2 years ago

@click2install are you able to provide a minimum reproducible example? I'd like to resolve this issue.