2

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.

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Phill
  • 18,398
  • 7
  • 62
  • 102
  • 2
    This answer touches on the same sort of general problem http://stackoverflow.com/a/28362898/126014 It doesn't discuss elements of collections, but rather elements (fields) of a single object, but the overall problem, as well as the solutions, is the same. – Mark Seemann Mar 26 '15 at 07:13
  • Here I am after a bunch of years on the same issue. I want to have a clean way to create a collection of test objects which will have different values. – Zigzagas Apr 14 '23 at 13:09

2 Answers2

2

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...
}
Mark Seemann
  • 225,310
  • 48
  • 427
  • 736
  • Because it doesn't seem to work in a customization. I don't want to have to configure 1 or more elements all the time. Example is i want to have a collection of values that have IsReadOnly property as true and some as false. Some which have priority and some which don't. This is used in quite a few places so can span 100s of tests. Setting up this data is very tedious, especially if I have use AF for most data then adhoc methods for other stuff. Also stuck with nunit. – Phill Mar 26 '15 at 08:16
  • As far as I understand what you write, you want the elements of a collection to have *variety*. Doesn't AutoFixture already give you that? – Mark Seemann Mar 26 '15 at 08:24
  • In a collection, inside a customization, I have no idea how to do it, no documentation or blog posts seem to help at all. I posted on github and they don't understand why anyone would want to modify elements in a collection. Would be nice if I could use AF to mock 5 objects, modify the few things I need, then add them back into a collection so CreateMany returns my defined list. – Phill Mar 26 '15 at 08:28
1
// 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!

AuthorProxy
  • 7,946
  • 3
  • 27
  • 40