How do I create a list of classes which always have a predefined value set in AutoFixture?

两盒软妹~` 提交于 2019-12-10 20:44:25

问题


how do I create a collection of classes which always have a certain predefined value set in AutoFixture?

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

Say the child class has the following:

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

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

How do I ensure the anonymous classes always have the same parent Id whilst all other properties can be random? I guess it would also be advisable to set ChildId to 0 too as these will be pushed into a database in a repository data test.


回答1:


Have you tried this?

fixture.Customize<Child>(c => c.With(x => x.ParentId, 42).With(x => x.ChildId, 0));


来源:https://stackoverflow.com/questions/5555054/how-do-i-create-a-list-of-classes-which-always-have-a-predefined-value-set-in-au

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!