Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/huysentruitw/entity-framework-core-mock
Easy Mock wrapper for mocking EFCore5 DbContext and DbSet using Moq or NSubstitute
https://github.com/huysentruitw/entity-framework-core-mock
csharp dbcontext dbset dotnet-standard dotnetcore efcore efcore5 entity-framework-core mock moq net50 nsubstitute unit-testing
Last synced: 3 months ago
JSON representation
Easy Mock wrapper for mocking EFCore5 DbContext and DbSet using Moq or NSubstitute
- Host: GitHub
- URL: https://github.com/huysentruitw/entity-framework-core-mock
- Owner: huysentruitw
- License: mit
- Created: 2018-09-24T19:57:37.000Z (over 6 years ago)
- Default Branch: develop
- Last Pushed: 2023-11-09T08:45:18.000Z (over 1 year ago)
- Last Synced: 2024-11-03T21:08:52.012Z (3 months ago)
- Topics: csharp, dbcontext, dbset, dotnet-standard, dotnetcore, efcore, efcore5, entity-framework-core, mock, moq, net50, nsubstitute, unit-testing
- Language: C#
- Homepage:
- Size: 170 KB
- Stars: 132
- Watchers: 5
- Forks: 24
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
- stars - huysentruitw/entity-framework-core-mock
README
# EntityFrameworkCoreMock
![Build status](https://github.com/cup-of-tea-dot-be/entity-framework-core-mock/actions/workflows/build-test-publish.yml/badge.svg?branch=master)
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](https://github.com/huysentruitw/entity-framework-core3-mock).
😮 Wait, did you say EF6? You really should get worried! Anyway, visit [this repository](https://github.com/huysentruitw/entity-framework-mock).
## 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](https://github.com/Moq/moq4/wiki/Quickstart) features, since both `DbSetMock` and `DbContextMock` inherit from `Mock` and `Mock` respectively.
## Example usage
```C#
public class User
{
[Key, Column(Order = 0)]
public Guid Id { get; set; }public string FullName { get; set; }
}public class TestDbContext : DbContext
{
public TestDbContext(DbContextOptions options)
: base(options)
{
}public virtual DbSet 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(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 DummyOptions { get; } = new DbContextOptionsBuilder().Options;
```