如何仅在列表中的子项目范围内分配属性? [英] How do I assign a property on only a subrange of items in a list?

查看:146
本文介绍了如何仅在列表中的子项目范围内分配属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用AutoFixture创建自定义对象的列表.我希望第一个N对象的属性设置为一个值,其余的将其设置为另一个值(或简单地由Fixture的默认策略设置).

I want to create a list of custom objects using AutoFixture. I want the first N objects to have a property set to one value, and the remainder to have it set to another value (or to simply be set by the Fixture's default strategy).

我知道我可以使用Fixture.CreateMany<T>.With,但这会将功能应用于列表的所有成员.

I am aware that I can use Fixture.CreateMany<T>.With, but this applies a function to all members of the list.

NBuilder中,有名为TheFirstTheNext的方法(以及其他方法)可以提供此功能.使用示例:

In NBuilder there are methods named TheFirst and TheNext (among others) which provide this functionality. An example of their use:

给出一个类Foo:

class Foo
{
    public string Bar {get; set;}
    public int Blub {get; set;}
}

一个人可以像这样实例化一堆Foo:

one can instantiate a bunch of Foos like so:

class TestSomethingUsingFoo
{
    /// ... set up etc.

    [Test]
    public static void TestTheFooUser()
    {
        var foosToSupplyToTheSUT = Builder<Foo>.CreateListOfSize(10)
            .TheFirst(5)
                .With(foo => foo.Bar = "Baz")
            .TheNext(3)
                .With(foo => foo.Bar = "Qux")
            .All()
                .With(foo => foo.Blub = 11)
            .Build();

        /// ... perform the test on the SUT 
    }
}

这将给出具有以下属性的类型为Foo的对象的列表:

This gives a list of objects of type Foo with the following properties:

[Object]    Foo.Bar    Foo.Blub
--------------------------------
0           Baz        10
1           Baz        10
2           Baz        10
3           Baz        10
4           Baz        10
5           Qux        10
6           Qux        10
7           Qux        10
8           Bar9       10
9           Bar10      10

(Bar9Bar10值表示NBuilder的默认命名方案)

(The Bar9 and Bar10 values represent NBuilder's default naming scheme)

是否有使用"AutoFixture"实现此目标的内置"方式?还是一种惯用的方式来设置灯具,使其表现得像这样?

Is there a 'built-in' way to achieve this using AutoFixture? Or an idiomatic way to set up a fixture to behave like this?

推荐答案

到目前为止,最简单的方法是这样的:

By far the easiest way to do that would be like this:

var foos = fixture.CreateMany<Foo>(10).ToList();
foos.Take(5).ToList().ForEach(f => f.Bar = "Baz");
foos.Skip(5).Take(3).ToList().ForEach(f => f.Bar = "Qux");
foos.ForEach(f => f.Blub = 11);

将值分配给属性已经内置在C#中,因此, AutoFixture的理念是使用现有的语言构造.

Assigning values to properties is already built-in to C#, so instead of providing a restrictive API that can't possibly enable you to do everything you'd want to do, the AutoFixture philosophy is to use the language constructs already available.

下一步的哲学步骤是,如果您经常需要执行类似的操作,则 SUT 可能会受益于重新设计.

The next philosophical step is that if you often need to do something like that, the SUT could probably benefit from a redesign.

这篇关于如何仅在列表中的子项目范围内分配属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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