【问题标题】:How to override default dependencies with Mockbox and Wirebox如何使用 Mockbox 和 Wirebox 覆盖默认依赖项
【发布时间】:2017-11-16 09:01:21
【问题描述】:

我有一个记录器服务,它具有 ScopeStorageLoggerServiceDAO 依赖项,在我的单元测试中,我需要覆盖这些以使用我创建的模拟对象。我正在使用 Wirebox AOP 来触发日志事件,所以我不能只创建一个模拟对象并将其传递给 CustomerContact 对象的构造函数

这是我正在创建的模拟:

scopeStorageMock = mockBox.createMock('system.ScopeStorage').$('get', 111);
loggerServiceDAOMock = mockBox.createMock('system.services.daos.loggerServiceDAO').$('insertLog');

在我的 Wirebox 活页夹中,我有以下映射:

map('CustomerContact').to('models.Customer.CustomerContactBean');
map('LoggerServiceDAO').to('system.Services.DAOs.LoggerServiceDAO');
map('ScopeStorage').to('system.ScopeStorage');
map('CustomerContactLogger').to('models.customer.loggers.CustomerContactLogger');
mapAspect("CustomerAspect").to('models.CustomerAspect');
bindAspect(classes=match().mappings("CustomerContact"), methods=match().methods(['create','delete', 'update']), aspects="CustomerContactLogger");

在我的单元测试中有没有办法告诉 Wirebox,当它获得 CustomerAspect 对象的实例时,可以使用我用 Mockbox 创建的两个模拟对象?

【问题讨论】:

    标签: unit-testing coldfusion coldbox wirebox testbox


    【解决方案1】:

    我找到了解决方案,虽然它看起来有点老套,但它确实有效。基本上我所做的是告诉 Wirebox 取消映射现有的 CustomerContactLogger,然后创建一个 CustomerContactLoggerMock 并注入所有 Mocked 属性。之后,我创建了 CustomerContactLogger 的新映射并将值设置为等于模拟对象。

    injector.getBinder().unMap('customercontactLogger');
    customerContactLoggerMock = mockBox.createMock('models.customer.loggers.CustomerContactLogger');
    customerContactLoggerMock.$property(propertyName='scopeStorage', mock=scopeStorageMock).$property(propertyName='loggerServiceDAO', mock=loggerServiceDAOMock);injector.getBinder().map('CustomerContactLogger').toValue(customerContactLoggerMock);
    injector.getBinder().map('CustomerContactLogger').toValue(customerContactLoggerMock);
    

    【讨论】:

      猜你喜欢
      • 2017-01-29
      • 1970-01-01
      • 2019-07-26
      • 2014-03-18
      • 1970-01-01
      • 2015-11-21
      • 2017-03-06
      • 2017-02-21
      • 2020-04-12
      相关资源
      最近更新 更多