سؤال

When looking at the post here, it looks like I should be able to create several objects using CreateMany(), iterate over them using foreach, and then return them as an array.

What I'm seeing is that each iteration seems to create new objects each time. Is this expected behavior?

Entity to create:

public class TestEntity
{
    public int Id { get; private set; }
    public string SomeString { get; set; }
    public void SetId(int value)
    {
        this.Id = value;
    }
}

Sample Program.cs:

private static int id;

static void Main(string[] args)
{
    var fixture = new Fixture();
    IEnumerable<TestEntity> testEntities = 
      fixture.Build<TestEntity>().CreateMany(5);

    Output(testEntities);

    foreach (var testEntity in testEntities)
    {
        testEntity.SetId(id++);
        Console.WriteLine(
          string.Format("CHANGED IN FOREACH:: hash: {0}, id: {1}, string: {2}", 
          testEntity.GetHashCode(), testEntity.Id, testEntity.SomeString));
    }

    Output(testEntities);
}

private static void Output(IEnumerable<TestEntity> testEntities)
{
    foreach (var testEntity in testEntities)
    {
        Console.WriteLine(
          string.Format("hash: {0}, id: {1}, string: {2}", 
          testEntity.GetHashCode(), testEntity.Id, testEntity.SomeString));
    }
}

I created an issue here (which can probably be deleted if this is expected behavior).

Edit 2011-06-02

To get the behavior I was expecting, and if I don't want to modify the AutoFixture behavior, I can use an extension method:

var fixture = new Fixture();
TestEntity[] testEntities = fixture.Build<TestEntity>().CreateMany(5).ToArray();
هل كانت مفيدة؟

المحلول

This is indeed the expected default behavior. There are many reasons for that, but basically it boils down to that when you ask for a IEnumerable<T> AutoFixture actually goes to great lengths to ensure that you get only what you ask for.

This is surprising behavior to many. The good news is that you can change it.

fixture.Customizations.Add(new StableFiniteSequenceRelay());

This will change the behavior such that subsequently all sequences are stable. You can package that method call into a Customization for better reusability. That might look something like this (totally optional):

public class StableFiniteSequenceCustomization : ICustomization
{
    public void Customize(IFixture fixture)
    {
        fixture.Customizations.Add(new StableFiniteSequenceRelay());
    }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top