C#
C#

help

Root Question Message

Fyren
Fyren12/8/2022
❔ Mocking with EntityFrameworkCore

Hm,
I'm creating unit tests for a class that has a private readonly MyDbContext dbContext; that causes a few problems.

InternalDbSet<MyEntity> invocation failed with mock behavior Strict.
All invocations on the mock must have a corresponding setup.


And you can't really mock that as you would everything else, because you cannot instantiate any DbSet collections as they don't have a non-private constructor.

Anybody who has mocked stuff with DbContexts on them? I'm not sure how to approach this.
Deluxe
Deluxe12/8/2022
dont mock it problem solved 🙂
Alexicon
Alexicon12/8/2022
Usually if I need to have a dbcontext for my unit tests, I will use the entity framework core in memory database rather than a mock. You can also use sqlite in memory if your using a relational database setup.
ContactFrequently Asked QuestionsJoin The DiscordBugs & Feature RequestsTerms & Privacy