我正在用 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();
}
}
这段代码中是否有代码异味?我该如何改进或简化这一点?