12

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();
Community
  • 1
  • 1
rbellamy
  • 5,683
  • 6
  • 38
  • 48

1 Answers1

9

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());
    }
}
Mark Seemann
  • 225,310
  • 48
  • 427
  • 736
  • 1
    Once again excellent help! I've accepted the answer, but it would be ++ if you could elaborate on what you mean by "AutoFixture actually goes to great lengths to ensure you get only what you ask for?" Namely, I probably need some education on what I'm asking for! :) – rbellamy May 31 '11 at 06:10
  • 2
    The deep explanation requires an entire blog post on its own (which *will* see the light of day in the future). The short answer is that the 'contract' of `IEnumerable` only specifies an Iterator. It's not the same as a List. Unstable Iterators or even Generators also fit the interface. – Mark Seemann May 31 '11 at 07:10
  • 2
    Yes, `yield` is part of the implementation :) – Mark Seemann May 31 '11 at 16:17
  • 6
    It turns out I already wrote that blog post and then forgot about it: http://blog.ploeh.dk/2011/04/18/EnumerablesAreDynamicalsoInAutoFixture.aspx – Mark Seemann May 31 '11 at 18:29
  • Outstanding. Thanks for a great library, and for the substantive help. – rbellamy Jun 02 '11 at 22:04
  • 2
    Update to the above link: http://blog.ploeh.dk/2011/04/18/EnumerablesaredynamicalsoinAutoFixture – Mark Seemann Jun 25 '13 at 20:27