All Projects → huysentruitw → entity-framework-mock

huysentruitw / entity-framework-mock

Licence: Apache-2.0 license
Easy Mock wrapper for mocking EF6 DbContext and DbSet using Moq or NSubstitute

Programming Languages

C#
18002 projects

Projects that are alternatives of or similar to entity-framework-mock

moq.ts
Moq for Typescript
Stars: ✭ 107 (+137.78%)
Mutual labels:  mock, moq
Hammox
🏝 automated contract testing via type checking for Elixir functions and mocks
Stars: ✭ 289 (+542.22%)
Mutual labels:  mock, unit-testing
InstantMock
Create mocks easily in Swift
Stars: ✭ 88 (+95.56%)
Mutual labels:  mock, unit-testing
Dotnetlabs
.NET Labs -- Show Me the Tips and Tricks and Code
Stars: ✭ 135 (+200%)
Mutual labels:  unit-testing, entity-framework
Unit Threaded
Advanced unit test framework for D
Stars: ✭ 100 (+122.22%)
Mutual labels:  mock, unit-testing
umock-c
A pure C mocking library
Stars: ✭ 29 (-35.56%)
Mutual labels:  mock, unit-testing
Rewire
Easy monkey-patching for node.js unit tests
Stars: ✭ 2,940 (+6433.33%)
Mutual labels:  mock, unit-testing
Onion Architecture Asp.net Core
WhiteApp API solution template which is built on Onion Architecture with all essential feature using .NET 5!
Stars: ✭ 196 (+335.56%)
Mutual labels:  unit-testing, entity-framework
Cuckoo
Boilerplate-free mocking framework for Swift!
Stars: ✭ 1,344 (+2886.67%)
Mutual labels:  mock, unit-testing
Service Pattern Go
Simple clean Go REST API architecture with dependency injection and mocking example, following SOLID principles.
Stars: ✭ 449 (+897.78%)
Mutual labels:  mock, unit-testing
contoso-university
Contoso University demo using asp net core and related technologies
Stars: ✭ 42 (-6.67%)
Mutual labels:  entity-framework, moq
Sinon Jest Cheatsheet
Some examples on how to achieve the same goal with either of both libraries: sinon and jest. Also some of those goals achievable only by one of these tools.
Stars: ✭ 226 (+402.22%)
Mutual labels:  mock, unit-testing
automock
A library for testing classes with auto mocking capabilities using jest-mock-extended
Stars: ✭ 26 (-42.22%)
Mutual labels:  mock, unit-testing
Firebase Mock
Firebase mock library for writing unit tests
Stars: ✭ 319 (+608.89%)
Mutual labels:  mock, unit-testing
Mocktopus
Mocking framework for Rust
Stars: ✭ 179 (+297.78%)
Mutual labels:  mock, unit-testing
EntityFrameworkCore.AutoFixture
A library aimed to minimize the boilerplate required to unit-test Entity Framework Core code using AutoFixture and in-memory providers.
Stars: ✭ 31 (-31.11%)
Mutual labels:  unit-testing, entity-framework
wab-test-example
ESRI Web App Builder Widget built using a wrapped widget convention and containing unit tests. This widget is meant to be used as an example to demonstrate the two techniques.
Stars: ✭ 21 (-53.33%)
Mutual labels:  unit-testing
ef-enterpriseextensions
EntityFramework.EnterpriseExtensions Library
Stars: ✭ 12 (-73.33%)
Mutual labels:  entity-framework
EntityFrameworkCore.Triggered
Triggers for EFCore. Respond to changes in your DbContext before and after they are committed to the database.
Stars: ✭ 361 (+702.22%)
Mutual labels:  entity-framework
eat
Json based scenario testing tool(which can have test for functional and non-functional)
Stars: ✭ 41 (-8.89%)
Mutual labels:  unit-testing

EntityFrameworkMock

Build status

Easy Mock wrapper for mocking EF6 DbContext and DbSet in your unit-tests. Integrates with Moq or NSubstitute.

For mocking Entity Framework Core (EF Core) see https://github.com/huysentruitw/entity-framework-core-mock

Get it on NuGet

Moq integration

PM> Install-Package EntityFrameworkMock.Moq

NSubstitute integration

PM> Install-Package EntityFrameworkMock.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
  • 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(string connectionString)
        : base(connectionString)
    {
    }

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

[TestFixture]
public class MyTests
{
    var initialEntities = new[]
        {
            new User { Id = Guid.NewGuid(), FullName = "Eric Cartoon" },
            new User { Id = Guid.NewGuid(), FullName = "Billy Jewel" },
        };

    var dbContextMock = new DbContextMock<TestDbContext>("fake connectionstring");
    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);
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].