Skip to content

Instantly share code, notes, and snippets.

@vdurante
Created January 19, 2022 17:44
Show Gist options
  • Save vdurante/2121cbafb82c2f12a38764f962a10ae5 to your computer and use it in GitHub Desktop.
Save vdurante/2121cbafb82c2f12a38764f962a10ae5 to your computer and use it in GitHub Desktop.
FakeItEasy DbContext issue
// 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