我正在尝试为我的域编写一套使用Entity Framework的数据库集成测试.在某些情况下,我更喜欢自动混合对象.我理想的语法就像是
[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();
}
}
Run Code Online (Sandbox Code Playgroud)
显然,这是行不通的,因为CreateAnonymous()不期望工厂的输入参数.我只能假设我对FromFactory()提供的内容有一个错误的理解.虽然评论上写着,
/// Specifies that a specimen should be created in …Run Code Online (Sandbox Code Playgroud)