【发布时间】:2015-11-10 16:39:29
【问题描述】:
给定两个类:
class Foo
{
...
}
class Bar
{
public Foo FooBar { get; set; }
}
我已经设置了以下测试:
void Test()
{
var fixture = new Fixture();
fixture.Customize<Foo>(x => x.FromSeed(TestFooFactory));
var fooWithoutSeed = fixture.Create<Foo>();
var fooWithSeed = fixture.Create<Foo>(new Foo());
var bar = fixture.Create<Bar>(); //error occurs here
}
Foo TestFooFactory(Foo seed)
{
//do something with seed...
return new Foo();
}
我可以直接创建带有和不带有种子值的Foo 对象,没有任何问题。但是一旦我尝试创建一个具有Foo 属性的Bar 对象,我就会得到一个ObjectCreationException:
装饰的 ISpecimenBuilder 无法根据请求创建样本:Foo.如果请求代表一个接口或抽象类,就会发生这种情况;如果是这种情况,请注册一个可以根据请求创建样本的 ISpecimenBuilder。如果这发生在强类型构建表达式中,请尝试使用 IFactoryComposer 方法之一提供工厂。
我希望TestFooFactory 在创建Bar 期间传递null 种子值,就像我在没有种子值的情况下创建Foo 时一样。我做错了什么,或者这可能是一个错误?
在我的真实场景中,当我传入种子值时,我想自定义 AutoFixture 如何为某些对象使用种子值,但如果没有提供种子,我仍然希望 AutoFixture 默认为正常行为。
【问题讨论】:
-
交叉发布到 GitHub:github.com/AutoFixture/AutoFixture/issues/467
标签: c# unit-testing autofixture