使用AutoFixture进行测试:解析ICollection <t>而不提及每一个T </t>

时间:2011-04-16 09:17:18

标签: c# entity-framework testing test-data autofixture

我尝试使用AutoFixture 2为具有ICollection成员的EntityFramework4类生成testdata。

    public class Parent
    {
        public virtual ICollection<Child1> Children1 { get; set; }
        public virtual ICollection<Child2> Children2 { get; set; }
        ...
        public virtual ICollection<Child759> Children759 { get; set; }
    }

    var factory = new Ploeh.AutoFixture.Fixture();
    var parent = factory.CreateAnonymous<Parent>();

由于AutoFixture无法解析ICollection<Child1>,我会收到Ploeh.AutoFixture.ObjectCreationException

我到目前为止找到的唯一解决方案是注册每个可能的“ICollection”,如此

    var factory = new Fixture();

    factory.Register<ICollection<Child1>>(() =>
        new List<Child1>());
    ...
    factory.Register<ICollection<Child759>>(() =>
        new List<Child759>());

    var parent = factory.CreateAnonymous<Parent>();

我的问题是

如果需要List<T>,是否有人知道某种方式或约定告诉AutoFixture始终使用ICollection<T>

1 个答案:

答案 0 :(得分:3)

AutoFixture 2.1将有conventions for various models of multiplicit y。计划是在GOTO Copenhagen之前获得2.1。

相关问题