带种子的 CreateMany 有什么作用?

What does CreateMany with seed do?

T seed 参数的 CreateMany 重载实际上做了什么?我试过播种,但种子似乎对创建的对象没有影响。例如,我期望如果我的种子有 string 类型的 属性,那么:

tl;博士

作为一般规则,AutoFixture does not guarantee how seed values are going to be used during the creation process, if at all. This characteristic stems from the way AutoFixture is designed.

背景

每次要求 AutoFixture 创建某个 Type 的对象时,请求都会通过 a pipeline of objects called "builders" 路由。每个构建器负责处理某种请求(无论是对具体类型的请求,还是接口属性字段等)。如果构建器遇到它可以处理的请求,它将 return 为它分配一个值,然后管道会在下一个请求时重新启动。

鉴于上述情况,如果您想基于 seed 创建对象,AutoFixture 所能做的就是确保您提供的种子值为 embedded in the request通过管道发送。然后由构建者决定如何处理该值。

解决方案

AutoFixture 当前带有一个 单一构建器 ,它考虑了种子值,即 the one for strings

但是,您可以告诉 AutoFixture 在创建 任何类型 的对象时应该如何使用种子值,方法是使用 [=26= 专门为该类型自定义 Fixture ]:

var fixture = new Fixture();
fixture.Customize<Foo>(c =>
    c.FromSeed(seed =>
        {
            // return an instance of Foo
            // that uses the seed value in some way
        }));

你提供给FromSeed工厂函数将在每次AutoFixture必须创建Foo的实例时被调用,并且它将被传递给种子来自 Fixture 的值。例如,鉴于此:

fixture.CreateMany<Foo>(seed: new Foo { Bar = "baz" });

工厂函数的 seed 参数将接收 Foo 种子对象,该对象的 Bar 属性 设置为 "baz".