Mocking Entity Framework DbContext with Moq – Mirko Maggioni
mirkomaggioni.com › 30 › ef-db-context-mock-with-moqAug 30, 2017 · Mocking Entity Framework DbContext with Moq. When we have to test methods that involves Entity Framework, a typical choice that we have to face is use integration tests, with an effective database, or unit tests. If we choice the first option, with a database like SQL LocalDB, we’ll have performance problems because the cost of the database creation and the data inserts in the test startup is very high, and in order to guarantee the initial conditions we’ll have to do it for each test.
c# - Moq IDBContextFactory with In-Memory EF Core - Stack ...
stackoverflow.com › questions › 66101618Feb 8, 2021 · Moq IDBContextFactory with In-Memory EF Core. I am testing a class that uses a DbContext. This class gets an IDbContextFactory injected, which is then used to get a DbContext: protected readonly IDbContextFactory<SomeDbContext> ContextFactory; public Repository (IDbContextFactory<SomeDbContext> contextFactory) { ContextFactory = contextFactory; } public List<T> Get () { using var db = ContextFactory.CreateDbContext (); return db.Set<T> ().ToList (); }
c# - Mocking EF DbContext with Moq - Stack Overflow
stackoverflow.com › questions › 25960192I created an interface IDbContext with the following functions: public interface IDbContext : IDisposable { IDbSet<T> Set<T> () where T : class; DbEntityEntry<T> Entry<T> (T entity) where T : class; int SaveChanges (); } My real context implements this interface IDbContext and DbContext. Now I'm trying to mock the IDbSet<T> in the context, so it returns a List<User> instead.