• Stars
    star
    132
  • Rank 273,040 (Top 6 %)
  • Language
    C#
  • License
    MIT License
  • Created about 6 years ago
  • Updated 11 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Easy Mock wrapper for mocking EFCore5 DbContext and DbSet using Moq or NSubstitute

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;

More Repositories

1

barcoder

Lightweight Barcode Encoding Library for .NET Framework, .NET Standard and .NET Core.
C#
152
star
2

SapNwRfc

SAP NetWeaver RFC library for .NET 5, .NET Core and .NET Framework
C#
143
star
3

print-it

Windows service for printing PDF files to a local or network printer in the background
C#
84
star
4

pem-utils

Managed .NET (C#) utility library for working with PEM files with DER/ASN.1 encoding
C#
68
star
5

win-beacon

Managed BT stack for Windows able to detect and act as an iBeacon.
C#
55
star
6

entity-framework-mock

Easy Mock wrapper for mocking EF6 DbContext and DbSet using Moq or NSubstitute
C#
48
star
7

cash-flow

Application for managing cash flows written in ASP.NET Core 6 and Angular 13 (EF Core, Apollo, GraphQL, CQRS)
C#
37
star
8

oauth2-client-handler

Managed .NET (C#) library for use with HttpClient to transparantly call authorized WebAPI
C#
35
star
9

Aggregator

Managed CQRS/ES fundamentals for dotnet core and dotnet framework
C#
31
star
10

process-manager

Managed library for managing and queing spawned processes
C#
27
star
11

svglib

SVG parser and composer library for .NET Framework and .NET Core
C#
11
star
12

owin-session-middleware

Cookie based session middleware for OWIN
C#
8
star
13

sepa-qr

.NET QR-Code generator for SEPA Payment Transactions
C#
4
star
14

owin-anti-forgery-middleware

OWIN Anti-Forgery (CSRF) middleware
C#
3
star
15

idento

Complete, manageable OpenID Connect server for adding Single Sign On (SSO) functionality to your project
C#
2
star
16

simple-json-api

Simple JSON API server implementation for ASP.NET Web API 2
C#
2
star
17

projecto

Managed .NET (C#) library for handling CQRS/ES projections while maintaining the event sequence order
C#
2
star
18

WaterTube

Library for filling a water tube and detecting the water level
C#
1
star
19

oracle-sql-executor

Executes SQL scripts in a folder against an Oracle database
C#
1
star