简体   繁体   English

在node.js中模拟sinon注入服务

[英]Mock with sinon injection services in node.js

I have the following class in a parent project (parent project will be a module in child project): 我在父项目中有以下类(父项目将是子项目中的模块):

export default class testService {

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

when I run test the test has been failed because the childservice does not exist. 当我运行测试时,由于子服务不存在,测试已失败。 the service is exist only if I open the child project that includes the parent module in the node modules 仅当我打开在节点模块中包含父模块的子项目时,该服务才存在

so my question is how can i mock that to prevent failure in test 所以我的问题是我该如何模拟它以防止测试失败

You can pass in a stub childService when instantiating the class in your test: 在实例化测试中的类时,可以传入存根childService

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM