2011-08-31 26 views
0

我覺得我已經迷失了我的想法,因爲我確信我之前完成了這個任務。希望你們中的一個能夠指引我正確的方向。在Rhino Mocks中設置列表的測試屬性

本質上,我有一個觀點,我想測試演示者是否正確地使用對象列表設置屬性。例如:

public ComplexDto{ 
    public string name{get;set;} 
    public string description{get;set;} 
} 

public interface ITestView{ 
IList<ComplexDto> dto{get;set;} 
} 

在演示這臺像一個列表:

... 
var dtos = new List<ComplexDto>(); 
dtos.add(new ComplexDto(){name="test name", description="this is some description"} 
view.dto = dtos; 
... 

我需要測試的DTO工作列表的那個那個內容。

我試過GetArgumentsForCallsMadeOn但我認爲這不適用於屬性。

感謝您的幫助!

回答

0

編輯

這應做到:

var view = MockRepository.GenerateMock<ITestView>(); 
var dtos = new List<ComplexDto>(); 
dtos.Add(new ComplexDto() {name = "test name", description = "this is some description"}); 

List<ComplexDto> passedIn; 
view.Expect(v => v.dto).SetPropertyWithArgument(dtos).WhenCalled(mi => passedIn = (List<ComplexDto>) mi.Arguments[0]); 

view.dto = dtos; 
view.VerifyAllExpectations(); 
+0

不幸的是,實際上不驗證清單的內容。它只驗證列表是否是相同的參考對象。如果創建兩個包含完全相同的ComplexDto值的單獨列表對象,它將失敗。我需要比較列表的內容以確保值是相同的。 ComplexDto,順便說一句,有散列和平等的遊樂設施,以確保它比較正確的相等。 – Chris

+0

查看我的更新回答。使用「WhenCalled」來獲取傳遞給屬性設置器的參數。現在,您可以對「passedIn」進行所需的任何驗證。 – PatrickSteele

+0

工程就像一個魅力。謝謝! – Chris