CreateMany where only 1 element contains certain value

浪子不回头ぞ 提交于 2019-12-24 13:18:21

问题


This seems to be something lacking in every blog post of piece of documentation I read.

How can I CreateMany<T> where only 1 of the elements in the collection contains an Id of something I specify?

I want to pull this back into a customization.

I tried many variations of something like this:

public class OrdersCustomizations : ICustomization
{
    public void Customize(IFixture fixture)
    {
        var single = fixture.Build<Order>().With(x => x.Id, 10).Create();

        var many = fixture.Build<Order>().CreateMany().ToList();
        many.Add(single);

        fixture.RepeatCount = 5;
        fixture.AddManyTo<Order>(many);
    }
}

But I can't figure this out.


回答1:


Why don't you just do the following?

var many = fixture.Create<List<Order>>();
many[0].Id = 10;

// Rest of test goes here...

Or, if you're using the declarative approach with AutoFixture.Xunit, you can do this:

public void MyTest(List<Order> orders)
{
    orders[0].Id = 10;

    // Rest of test goes here...
}



回答2:


// Configure fixture
var expectedResult = _fixture.Create<List<Order>>();
expectedResult[0].Id = "159";
_fixture.Inject(expectedResult);

// Usage
var actualResult = _fixture.Create<List<Order>>();
Assert.True(actualResult[0].Id == expectedResult[0].Id);

This is what you are searching!



来源:https://stackoverflow.com/questions/29270580/createmany-where-only-1-element-contains-certain-value

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!