I\'m trying to write a suite of database integration tests for my domain which uses Entity Framework. I would prefer to autofixture objects in some scenarios. My ideal synt
Would something like this work?
var fixture = new Fixture();
fixture.Customize<MyEntity>(c => c
.FromFactory<MyDbContext, MyEntity>(ctx => ctx.Set<MyEntity>.Create()));
using (var context = new MyDbContext())
{
fixture.Inject(context);
var item = fixture.CreateAnonymous<MyEntity>();
context.Set<MyEntity>().Add(item);
context.SaveChanges();
}
Disclaimer: I haven't tried to compile this...
FWIW, if you were using xUnit.net with AutoFixture, you could reduce the test to something like:
[Theory, MyAutoData]
public void TheTest([Frozen]MyDbContext context, MyEntity item)
{
context.Set<MyEntity>().Add(item);
context.SaveChanges();
}