Fixture是否可以创建N个对象的列表?

时间:2014-10-10 12:20:00

标签: c# fixture

我想用Fixture创建一个N个对象的列表。

我知道我可以用:

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

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

有什么方法可以使用CreateMany()方法或其他方法来避免循环?

5 个答案:

答案 0 :(得分:2)

找到答案。 CreateMany有一些重载,可以“计数”。

谢谢大家。

答案 1 :(得分:2)

你可以使用linq:

  List<Person> persons = Enumerable.Range(0, numberOfPersons)
            .Select(x => fixture.Build<Person>().Create())
            .ToList();

答案 2 :(得分:0)

我做过人。

/// <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
}

答案 3 :(得分:0)

let date = Date(timeIntervalSince1970: TimeInterval(timeInMiliFromBackEnd/1000))
let interval = date.timeIntervalSince1970
let url = NSURL(string: "calshow:\(interval)")!
        UIApplication.shared.openURL(url as URL)

答案 4 :(得分:0)

是的,可以将CreateMany用作下一个示例:

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