我正在爲MVP模式中的演示文稿類編寫單元測試。但我無法編寫模擬設置代碼。單元測試中如何處理太多的模擬期望?
我有一個演示者,當演示者的Load方法調用時,我想測試視圖應該加載類屬性,表字段,數據類型,設置演示者....所以當我有一個不同的事情時,演示者加載總是我必須增加新的期望來測試。每次測試都在變得越來越大。
[Test]
public void When_Presenter_Loads_View_Should_Display_Selected_Class_Properties()
{
IList<string> dataTypes =new List<string>();
IClassGenerationView view = mockRepository.StrictMock<IClassGenerationView>();
tableRepository = mockRepository.Stub<ITableRepository>();
using(mockRepository.Record())
{
SetupResult.For(tableRepository.GetDataTypes()).Return(dataTypes);
view.Presenter = null;
LastCall.IgnoreArguments();
view.DataTypes = dataTypes;
view.Show();
view.ClassProperties = classProperties;
view.TableName = "Table";
view.Table = table;
LastCall.IgnoreArguments();
}
using(mockRepository.Playback())
{
ClassGenerationPresenter presenter = new ClassGenerationPresenter(view, clazz, tableRepository);
presenter.Load();
}
}
這段代碼有沒有代碼味道?我如何改進或簡化這個?
您能否將此答案標記爲正確答案?謝謝! – guerda 2009-01-07 10:14:49