Code Monkey home page Code Monkey logo

entity-framework-core-mock's Introduction

EntityFrameworkCoreMock

Build status

Easy Mock wrapper for mocking EntityFrameworkCore 5 (EFCore5) DbContext and DbSet in your unit-tests. Integrates with Moq or NSubstitute.

๐Ÿ˜ข Are you still stuck on EF Core 3.1? No worries, just visit this repository.

๐Ÿ˜ฎ Wait, did you say EF6? You really should get worried! Anyway, visit this repository.

Get it on NuGet

Moq integration

PM> Install-Package EntityFrameworkCoreMock.Moq

NSubstitute integration

PM> Install-Package EntityFrameworkCoreMock.NSubstitute

Supports

  • In-memory storage of test data
  • Querying of in-memory test data (synchronous or asynchronous)
  • Tracking of updates, inserts and deletes of in-memory test data
  • Emulation of SaveChanges and SaveChangesAsync (only saves tracked changes to the mocked in-memory DbSet when one of these methods are called)
  • Auto-increment identity columns, annotated by the [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)] attribute
  • Primary key on multiple columns, annotated by the [Key, Column(Order = X)] attributes

TODO

  • Throwing a DbUpdateException when inserting 2 or more entities with the same primary key while calling SaveChanges / SaveChangesAsync (emulating EF behavior)
  • Throwing a DbUpdateConcurrencyException when removing a model that no longer exists (emulating EF behavior)

For the Moq version, you can use all known Moq features, since both DbSetMock and DbContextMock inherit from Mock<DbSet> and Mock<DbContext> respectively.

Example usage

public class User
{
    [Key, Column(Order = 0)]
    public Guid Id { get; set; }

    public string FullName { get; set; }
}

public class TestDbContext : DbContext
{
    public TestDbContext(DbContextOptions<TestDbContext> options)
        : base(options)
    {
    }

    public virtual DbSet<User> Users { get; set; }
}

public class MyTests
{
    [Fact]
    public void DbSetTest()
    {
        var initialEntities = new[]
            {
                new User { Id = Guid.NewGuid(), FullName = "Eric Cartoon" },
                new User { Id = Guid.NewGuid(), FullName = "Billy Jewel" },
            };
        
        var dbContextMock = new DbContextMock<TestDbContext>(DummyOptions);
        var usersDbSetMock = dbContextMock.CreateDbSetMock(x => x.Users, initialEntities);
    
        // Pass dbContextMock.Object to the class/method you want to test
    
        // Query dbContextMock.Object.Users to see if certain users were added or removed
        // or use Mock Verify functionality to verify if certain methods were called: usersDbSetMock.Verify(x => x.Add(...), Times.Once);
    }
}

public DbContextOptions<TestDbContext> DummyOptions { get; } = new DbContextOptionsBuilder<TestDbContext>().Options;

entity-framework-core-mock's People

Contributors

alvarollmenezes avatar gabrielrobert avatar huysentruitw avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.