【问题标题】:Mock with sinon injection services in node.js在 node.js 中模拟 sinon 注入服务
【发布时间】:2018-06-25 14:16:21
【问题描述】:

我在父项目中有以下类(父项目将是子项目中的模块):

export default class testService {

    constructor({loggerFactory,childService}) {
        this.logger = loggerFactory.logger
        this.child = childService;
    }
}

当我运行测试时,测试失败了,因为子服务不存在。 只有当我在节点模块中打开包含父模块的子项目时,该服务才存在

所以我的问题是如何模拟它以防止测试失败

【问题讨论】:

    标签: javascript node.js testing sinon


    【解决方案1】:

    在测试中实例化类时,您可以传入存根 childService

    const loggerFactory = {
      logger: () => {},
    }
    
    const childService = {}
    
    const mockTestService = new testService({loggerFactory, childService});

    【讨论】:

      猜你喜欢
      • 2013-04-07
      • 1970-01-01
      • 2015-10-25
      • 2013-01-24
      • 1970-01-01
      • 2017-01-20
      • 2015-05-08
      • 2018-06-16
      • 2021-04-05
      相关资源
      最近更新 更多