2010-02-03 88 views
2

在呈現者的方法中,我期望調用一個視圖方法。該方法還傳遞從服務方法中提取的數據(未被模擬)。該服務方法基本上獲取數據從數據庫返回表(使用LINQ to SQL)。現在,當我寫這篇文章的測試無法找出犀牛模擬問題

List<customers> cus = expecteddata; 
view.AssertWasCalled(v => v.InitializeCustomersForSelectedCity(cus));  

Rhino.Mocks.Exceptions.ExpectationViolationException: ICustomerListView.InitializeCustomersForSelectedCity(System.Collections.Generic.List`1[DAL.Customer]); Expected #1, Actual #0. 

我在主持人正在測試的代碼

public void HandleSelectedCity(int City) 
    { 
     selectedCity = City ; 
     _custometListForm.InitializeCustomersForSelectedCity(_CustomerListService.GetActiveCustomersForSelectedCity(selectedCity));    
    } 

當我忽略的參數,測試工作罰款 可能是什麼問題?

回答

3

您斷言根據cus創建一個期望值,這是在單元測試中定義的一個變量。但是,當調用InitializeCustomersForSelectedCity時,將調用GetActiveCustomersForSelectedCity的結果 - List<customers>的另一個實例。

期望設置基本上對預期實例和實際實例執行object.Equals操作。在你的情況下,他們是不同的,期望行爲不滿意。

要麼你需要放鬆你的期望接受任何List<customers>,或者你也需要模擬GetActiveCustomersForSelectedCity,以便你可以定義從單元測試返回的結果。

+0

好的,謝謝。我決定把測試分成2 - 1個互動測試,方法被調用。而且,另一個測試服務方法 – 2010-02-03 06:58:11