这是什么CreateMany种子呢? [英] What does CreateMany with seed do?

查看:293
本文介绍了这是什么CreateMany种子呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是 CreateMany 超载与 T种子参数实际上呢?我试着种子,但种子似乎对创建的对象没有任何影响。例如,我期待,如果我的种子有类型的属性字符串,要么:




  • 的字符串值将被用于填充在所有新创建的对象属性


  • 的字符串值将被用作一个或前缀在所有新创建的对象设置该属性时



解决方案

TL;博士



作为一般规则,AutoFixture 不保证种子值是怎么回事,如果在所有要在创建过程中使用。这种特性从AutoFixture 设计的方式造成的。



背景



每次AutoFixture被要求创建的对象一定键入中,请求通过称为对象的管道建设者。每个构建器负责处理某一种的请求的(无论它是一个的具体类型的,要求一个的接口的,一个的属性的,一个的字段的等)。如果一个建设者遇到它可以处理一个请求,它会返回它和值与下一个请求管道重新启动。



鉴于上述情况,如果你想创建一个基于的种子的对象,所有的AutoFixture所能做的就是确保你提供的种子值的的该请求/ SeededRequest.cs相对=nofollow>通过管道发送。然后,它是给建设者们决定如何处理该值做的。



解决方案



AutoFixture目前自带一个的单生成器的,考虑到种子值,这就是的一个字符串的。



不过,你能不能告诉AutoFixture创建对象时,应该如何使用的种子值的任何类型的通过自定义灯具专为与类型的 FromSeed 方法



<预类=郎-CS prettyprint-覆盖> VAR夹具=新灯();
fixture.Customize<富>(C =>
c.FromSeed(种子=>
{
//返回美孚
//的一个实例使用种子值以某种方式
}));



工厂函数的你提供给 FromSeed 将被调用每一个AutoFixture具有创建富的时间,它会被传递从灯具<种子值/ code>。例如,给定这样的:



<预类=郎-CS prettyprint-覆盖> fixture.CreateMany<富>(种子:新富{酒吧=巴兹});工厂功能的



种子参数将收到具有种子对象属性设置为巴兹


What does the CreateMany overload with the T seed parameter actually do? I've tried to seed, but the seed seems to have no effect on the created objects. For example, I was expecting that if my seed had a property of type string, that either:

  • the string value would be used to populate that property in all newly created objects

  • or the string value would be used as a prefix when setting that property in all newly created objects

解决方案

tl;dr

As a general rule, 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.

Background

Every time AutoFixture is asked to create an object of a certain Type, the request is routed through a pipeline of objects called "builders". Each builder is responsible for handling a certain kind of request (whether it be a request for a concrete type, an interface, a property, a field etc). If a builder encounters a request it can handle, it's going to return a value for it and the pipeline restarts with the next request.

Given the above, if you want to create an object based on a seed, all AutoFixture can do is make sure that the seed value you provide is embedded in the request that is sent through the pipeline. Then it's up to the builders to decide what to do with that value.

Solution

AutoFixture currently comes with a single builder that takes into account seed values and that's the one for strings.

However, you can tell AutoFixture how it should use seed values when creating objects of any type by customizing a Fixture specifically for that type with the FromSeed method:

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

The factory function you provide to FromSeed will be invoked every time AutoFixture has to create an instance of Foo and it will be passed the seed value from the Fixture. For example, given this:

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

the seed parameter of the factory function will receive the Foo seed object that has the Bar property set to "baz".

这篇关于这是什么CreateMany种子呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆