gpt4 book ai didi

c# - 使用 FromSeed 自定义 AutoFixure 导致异常

转载 作者:太空狗 更新时间:2023-10-29 23:13:41 29 4
gpt4 key购买 nike

给定两个类:

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:

The decorated ISpecimenBuilder could not create a specimen based on the request: Foo. This can happen if the request represents an interface or abstract class; if this is the case, register an ISpecimenBuilder that can create specimens based on the request. If this happens in a strongly typed Build expression, try supplying a factory using one of the IFactoryComposer methods.

我希望 TestFooFactory 在创建 Bar 期间传递一个 null 种子值,就像我创建 时一样Foo 没有种子值。我做错了什么,或者这可能是一个错误?

在我的真实场景中,我想自定义当我传入种子值时 AutoFixture 如何为某些对象使用种子值,但我仍然希望 AutoFixture 在没有提供种子时默认为正常行为。

最佳答案

自定义 Fixture 以使用种子值的方式 is correct .

您看到的行为是 FromSeed 自定义修改 AutoFixture 管道的结果。如果您有兴趣阅读详细信息,我已经对其进行了描述 here .

作为一种解决方法,您可以使用自定义样本生成器来处理像这样的种子请求:

public class RelaxedSeededFactory<T> : ISpecimenBuilder
{
private readonly Func<T, T> create;

public RelaxedSeededFactory(Func<T, T> factory)
{
this.create = factory;
}

public object Create(object request, ISpecimenContext context)
{
if (request != null && request.Equals(typeof(T)))
{
return this.create(default(T));
}

var seededRequest = request as SeededRequest;

if (seededRequest == null)
{
return new NoSpecimen(request);
}

if (!seededRequest.Request.Equals(typeof(T)))
{
return new NoSpecimen(request);
}

if ((seededRequest.Seed != null)
&& !(seededRequest.Seed is T))
{
return new NoSpecimen(request);
}

var seed = (T)seededRequest.Seed;

return this.create(seed);
}
}

然后您可以使用它来创建类型为 Foo 的对象,如下所示:

fixture.Customize<Foo>(c => c.FromFactory(
new RelaxedSeededFactory<Foo>(TestFooFactory)));

此自定义将传递 default(Foo)——即 null——在填充类型的属性时作为 TestFooFactory 工厂函数的种子Foo.

关于c# - 使用 FromSeed 自定义 AutoFixure 导致异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33635042/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com