Is it possible with Fixture to create a list of N objects?
Asked Answered
V

7

7

I want to create with Fixture a list of N objects.

I know I can do it with:

List<Person> persons = new List<Person>();

for (int i = 0; i < numberOfPersons; i++)
{
    Person person = fixture.Build<Person>().Create();
    persons.Add(person);
}

Is there any way I can use the CreateMany() method or some other method in order to avoid the loop?

Vania answered 10/10, 2014 at 12:20 Comment(0)
V
0

I have done it people.

/// <summary>
/// This is a class containing extension methods for AutoFixture.
/// </summary>
public static class AutoFixtureExtensions
{
    #region Extension Methods For IPostprocessComposer<T>

    public static IEnumerable<T> CreateSome<T>(this IPostprocessComposer<T> composer, int numberOfObjects)
    {
        if (numberOfObjects < 0)
        {
            throw new ArgumentException("The number of objects is negative!");
        }

        IList<T> collection = new List<T>();

        for (int i = 0; i < numberOfObjects; i++)
        {
            collection.Add(composer.Create<T>());
        }

        return collection;
    }

    #endregion
}
Vania answered 10/10, 2014 at 12:56 Comment(0)
V
8

Found the answer. CreateMany has some overloads that get 'count'.

Thanks people.

Vania answered 10/10, 2014 at 12:22 Comment(2)
how did you find the answer just within 100 seconds since you posted the question !! :PCollect
When 'count' is zero it does not return an empty collection but it throws an expception. Bad implementation.Vania
E
6

you can use linq:

  List<Person> persons = Enumerable.Range(0, numberOfPersons)
            .Select(x => fixture.Build<Person>().Create())
            .ToList();
Ellen answered 10/10, 2014 at 12:22 Comment(4)
I will accept it for now. But, I would like a solution with a more Fixture way rather than LINQ.Vania
Not working. It shows a warning that Select has two overloaded methods.Vania
Thanks. Is there a solution by using only Fixture?Vania
Nowaday at least you can also, in a one liner, adapt any property using linq: List<Person> persons = Enumerable.Range(0, numberOfPersons) .Select(x => fixture.Build<Person>() .With(p => p.Name, "Some name") .With(p => p.Address, "Some address").Create()) .ToList();Guttersnipe
C
6

Yes Sure, you can use the CreateMany as the next sample:

var numberOfPersons = 10; //Or your loop length number
var fixture = new Fixture();
var person = fixture.CreateMany<Person>(numberOfPersons).ToList(); 
//ToList() to change  the IEnumerable to List
Cissy answered 23/1, 2019 at 11:14 Comment(0)
E
2
var people = _fixture
        .Build<Person>()
        .CreateMany()
        .ToList(); // if you want to return a generic list
Easement answered 17/2, 2022 at 17:37 Comment(2)
Hey Enes, could you maybe elaborate a bit on the answer?Hypso
hi sorry for the late response. You can send an integer value to the CreateMany() function. it would return you that amount of instance you want.Easement
S
1
var dtos = (new Fixture()).CreateMany<YourObjectType>(numberRecords);
Symposiac answered 18/1, 2018 at 2:5 Comment(0)
V
0

I have done it people.

/// <summary>
/// This is a class containing extension methods for AutoFixture.
/// </summary>
public static class AutoFixtureExtensions
{
    #region Extension Methods For IPostprocessComposer<T>

    public static IEnumerable<T> CreateSome<T>(this IPostprocessComposer<T> composer, int numberOfObjects)
    {
        if (numberOfObjects < 0)
        {
            throw new ArgumentException("The number of objects is negative!");
        }

        IList<T> collection = new List<T>();

        for (int i = 0; i < numberOfObjects; i++)
        {
            collection.Add(composer.Create<T>());
        }

        return collection;
    }

    #endregion
}
Vania answered 10/10, 2014 at 12:56 Comment(0)
B
0

You can use AutoFixture.CreateMany(n) where n is the numbers of items you want :

var persons = Fixture.CreateMany<Person>(100).ToList();
//persons.Count() = 100

You can also configure AutoFixture with pre-configured numbers of items with RepeatCount :

fixture.RepeatCount = 100;
var persons = fixture.CreateMany<Person>().ToList();
//persons.Count() = 100
Barta answered 15/4, 2022 at 13:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.