Created
January 19, 2022 17:44
-
-
Save vdurante/2121cbafb82c2f12a38764f962a10ae5 to your computer and use it in GitHub Desktop.
FakeItEasy DbContext issue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// create a dotnet 6 project with this file and run A_Fake_Service test | |
using FakeItEasy; | |
using Microsoft.EntityFrameworkCore; | |
using Xunit; | |
namespace FakeItIssue; | |
// declare a DbContext normally | |
public class MyDbContext : DbContext | |
{ | |
public MyDbContext(DbContextOptions<MyDbContext> options) | |
: base(options) | |
{ | |
} | |
} | |
// declare a service with interface | |
public interface IMyService | |
{ | |
} | |
public class MyService : IMyService | |
{ | |
private readonly MyDbContext _dbContext; | |
public MyService(MyDbContext dbContext) | |
{ | |
_dbContext = dbContext; | |
} | |
} | |
public class Tests | |
{ | |
[Fact] | |
public void A_Fake_Service() | |
{ | |
// Succeeds | |
A.Fake<IMyService>(); | |
// Fails | |
A.Fake<MyService>(); | |
/* | |
* System.InvalidOperationException: The DbContextOptions passed to the MyDbContextProxy constructor must be a DbContextOptions<MyDbContextProxy>. | |
* When registering multiple DbContext types, make sure that the constructor for each context type has a DbContextOptions<TContext> parameter rather | |
* than a non-generic DbContextOptions parameter. | |
*/ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment