TDD和DI:依赖注射变得麻烦

前端之家收集整理的这篇文章主要介绍了TDD和DI:依赖注射变得麻烦前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
C#,nUnit和Rhino Mocks,如果证明是适用的.

我的TDD继续尝试围绕一个复杂的功能进行测试.假设我编写一个表单,当保存时,还必须在表单中保存依赖对象…答案,以形成问题,附件(如果可用)和“日志”条目(例如“blahblah更新表单”)或“blahblah附上一个文件”).这种保存功能还可以根据在保存功能中更改形式的状态,向各种人员发送电子邮件.

这意味着为了充分测试表单的所有依赖关系的保存功能,我必须注入5到6个数据提供程序来测试这一个函数,并确保所有内容都以正确的方式发布并进行排序.在编写表单对象的多个链接构造函数以插入嘲笑的提供程序时,这很麻烦.我想我缺少一些东西,无论是重构的方式还是简单的更好的方式设置嘲弄的数据提供者.

我应该进一步研究重构方法,看看这个功能如何被简化?观察者模式如何发声,从而依赖对象检测父表单何时被保存并处理自身?我知道人们说要分解这个函数,以便它可以被测试…意味着我测试每个依赖对象的个人保存功能,而不是表单本身的保存功能,这决定了每个应该如何保存在第一名?

使用AutoMocking容器.有一个为RhinoMock写的.

想象一下,你有一个类通过构造器注入注入了很多依赖.下面是用RhinoMocks来设置它,而不是AutoMocking容器:

private MockRepository _mocks;
private BroadcastListViewPresenter _presenter;
private IBroadcastListView _view;
private IAddNewBroadcastEventBroker _addNewBroadcastEventBroker;
private IBroadcastService _broadcastService;
private IChannelService _channelService;
private IDeviceService _deviceService;
private IDialogFactory _dialogFactory;
private IMessageBoxService _messageBoxService;
private ITouchScreenService _touchScreenService;
private IDeviceBroadcastFactory _deviceBroadcastFactory;
private IFileBroadcastFactory _fileBroadcastFactory;
private IBroadcastServiceCallback _broadcastServiceCallback;
private IChannelServiceCallback _channelServiceCallback;

[SetUp]
public void SetUp()
{
    _mocks = new MockRepository();
    _view = _mocks.DynamicMock<IBroadcastListView>();

    _addNewBroadcastEventBroker = _mocks.DynamicMock<IAddNewBroadcastEventBroker>();

    _broadcastService = _mocks.DynamicMock<IBroadcastService>();
    _channelService = _mocks.DynamicMock<IChannelService>();
    _deviceService = _mocks.DynamicMock<IDeviceService>();
    _dialogFactory = _mocks.DynamicMock<IDialogFactory>();
    _messageBoxService = _mocks.DynamicMock<IMessageBoxService>();
    _touchScreenService = _mocks.DynamicMock<ITouchScreenService>();
    _deviceBroadcastFactory = _mocks.DynamicMock<IDeviceBroadcastFactory>();
    _fileBroadcastFactory = _mocks.DynamicMock<IFileBroadcastFactory>();
    _broadcastServiceCallback = _mocks.DynamicMock<IBroadcastServiceCallback>();
    _channelServiceCallback = _mocks.DynamicMock<IChannelServiceCallback>();


    _presenter = new BroadcastListViewPresenter(
        _addNewBroadcastEventBroker,_broadcastService,_channelService,_deviceService,_dialogFactory,_messageBoxService,_touchScreenService,_deviceBroadcastFactory,_fileBroadcastFactory,_broadcastServiceCallback,_channelServiceCallback);

    _presenter.View = _view;
}

现在,AutoMocking容器也是一样的:

private MockRepository _mocks;
private AutoMockingContainer _container;
private BroadcastListViewPresenter _presenter;
private IBroadcastListView _view;

[SetUp]
public void SetUp()
{

    _mocks = new MockRepository();
    _container = new AutoMockingContainer(_mocks);
    _container.Initialize();

    _view = _mocks.DynamicMock<IBroadcastListView>();
    _presenter = _container.Create<BroadcastListViewPresenter>();
    _presenter.View = _view;

}

更容易吗

AutoMocking容器会自动为构造函数中的每个依赖项创建嘲讽,您可以访问它们进行测试,如下所示:

using (_mocks.Record())
    {
      _container.Get<IChannelService>().Expect(cs => cs.ChannelIsBroadcasting(channel)).Return(false);
      _container.Get<IBroadcastService>().Expect(bs => bs.Start(8));
    }

希望有帮助.我知道,随着AutoMocking容器的出现,我的测试生活已经变得非常简单了.

猜你在找的设计模式相关文章