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 syntax would be something like
[TestMethod]
public void AutofixtureMyEntityEntity()
{
var fixture = new Fixture();
fixture.Customize<MyEntity>(
c => c.FromFactory<MyDbContext>(ctx => ctx.Set<MyEntity>().Create()));
using (var context = new MyDbContext())
{
fixture.Inject(context);
var entity = fixture.CreateAnonymous<MyEntity>();
context.Set<MyEntity>().Add(entity);
context.SaveChanges();
}
}
[TestMethod]
[ExpectedException(typeof(InvalidOperationException))]
public void AutoFixtureMyEntityEntityWithoutInjection()
{
var fixture = new Fixture();
fixture.Customize<MyEntity>(
c => c.FromFactory<MyDbContext>(ctx => ctx.Set<MyEntity>().Create()));
using (var context = new MyDbContext())
{
var entity = fixture.CreateAnonymous<MyEntity>();
context.Set<MyEntity>().Add(entity);
context.SaveChanges();
}
}
Obviously, that isn't working since CreateAnonymous()
isn't expecting the input parameter for the factory. I can only assume that i have a flawed understanding of what FromFactory()
provides. Although the comment reads,
/// Specifies that a specimen should be created in a particular way, using a single input
/// parameter for the factory.
After reading ploehs blog, I'm slightly more confused on how these pieces interact with each other.
The instance of MyDbContext
during factory invocation is not the instance I passed to Inject()