3

如何创建一个始终在 AutoFixture 中设置某个预定义值的类的集合?

Fixture.Register<IList<Child>>(() => Fixture.CreateMany<Child>().ToList());        

假设子类具有以下内容:

public class Child
{
    public int ParentId { get; set; }
    public int ChildId { get; set; }

    public string ChildName { get; set; }
    public int ChildValue { get; set; }
}

如何确保匿名类始终具有相同的父 ID,而所有其他属性都可以是随机的?我想将 ChildId 设置为 0 也是可取的,因为这些将被推送到存储库数据测试中的数据库中。

4

1 回答 1

1

你试过这个吗?

fixture.Customize<Child>(c => c.With(x => x.ParentId, 42).With(x => x.ChildId, 0));
于 2011-04-05T18:35:06.010 回答