我试着写了一套数据库集成测试我的域,它使用实体框架。 我宁愿autofixture在某些场景中的对象。 我理想中的语法会是这样的
[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();
}
}
显然,这是不是因为工作CreateAnonymous()
没有预料到的工厂输入参数。 我只能假设我有一个什么样的有缺陷的理解FromFactory()
提供。 虽然评论读取,
/// Specifies that a specimen should be created in a particular way, using a single input
/// parameter for the factory.
看完后ploehs博客 ,我对这些作品是如何互相影响稍微困惑。
实例MyDbContext
工厂调用过程中是不是我传给实例Inject()