2

i need to mock EF for my unit tests. Many tests are already working, but now i'm in a test that needs to mock the Database-Object in my DbContext, because the MyDbContext.Database.ExecuteSqlCommand is called.

Like this (almost 3 years ago and unanswered) Moq Entity Frameworks ExecuteSQLCommand

But with NSubstitue.

UPDATE:

I'm using the identity framework. So if nsubstitute is not throwing an exception itself, i get an exception from the identity framework

Castle.Proxies.IdentityUserLogin: : EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType.
Castle.Proxies.IdentityUserRole: : EntityType 'IdentityUserRole' has no key defined. Define the key for this EntityType.

I tried all the following:

DBContextMock.When(x => { var get = x.Database; }).DoNotCallBase();
DBContextMock.When(x => { var get = x.Database.ExecuteSqlCommand("UPDATE"); }).DoNotCallBase();
DBContextMock.Database.ExecuteSqlCommand("UPDATE").ReturnsForAnyArgs(0);

Any ideas to mock the Database object?

Dosihris
  • 85
  • 1
  • 8
  • I've got the same problem. Anyone have any ideas? I did a lot of searching and can only find a package called Effort to help with this. I can't believe this isn't a common problem though – Jason Cheng Apr 25 '22 at 02:03

0 Answers0