2011-04-05 35 views
3

如何創建一個總是在AutoFixture中設置特定預定義值的類的集合?如何創建一個總是在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,因爲它們將被推入存儲庫數據測試中的數據庫中。

回答

1

你試過嗎?

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

嗨,謝謝你。我實際上只是決定鏈接2.Without(),因爲我使用的EF代碼優先模型同時在Child類中定義了Child PK和Parent FK。我在2 Without()語句中將這兩個設置爲0,並在EF插入後讓EF填充它們。 – jaffa 2011-04-06 22:56:41

相關問題