Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 模拟需求(ed)服务_Javascript_Node.js_Unit Testing_Mocking_Sinon - Fatal编程技术网

Javascript 模拟需求(ed)服务

Javascript 模拟需求(ed)服务,javascript,node.js,unit-testing,mocking,sinon,Javascript,Node.js,Unit Testing,Mocking,Sinon,我试图使用sinon模拟我的函数使用的服务,但我找不到将模拟注入函数的方法 使用require获取服务 我的功能是这样的: // something.js const service = require('./service'); module.exports = { // do something using a service doSomething: function (options) { let results = []; return

我试图使用sinon模拟我的函数使用的服务,但我找不到将模拟注入函数的方法

使用require获取服务

我的功能是这样的:

// something.js
const service = require('./service');
module.exports = {
    // do something using a service
    doSomething: function (options) {
        let results = [];
        return service.foo(option.param)
        // and return the value adding some logic
        .then(function(resultFromService){
            // want to test this flag
            if ( options.someFlag ) {
                results = resultFromService;
            }
        })
        .then(function(){
            /// more existing code here
            return results;
         });
    } 
}
const something = require('../../something.js');
const sinon = require('sinon');  
...
it('should doSomething by calling foo in the service', function(done) {

     ///I'm getting this error here: Error: Trying to stub property 'foo' of undefined
     sinon.stub( something.service , 'foo' )
     .returns(when(function() {
         return ['bar', 'baz'];
     }));


     let promise = something.doSomething({param:'for service', someFlag:true});
});
我试图模仿service.foo,如下所示:

// something.js
const service = require('./service');
module.exports = {
    // do something using a service
    doSomething: function (options) {
        let results = [];
        return service.foo(option.param)
        // and return the value adding some logic
        .then(function(resultFromService){
            // want to test this flag
            if ( options.someFlag ) {
                results = resultFromService;
            }
        })
        .then(function(){
            /// more existing code here
            return results;
         });
    } 
}
const something = require('../../something.js');
const sinon = require('sinon');  
...
it('should doSomething by calling foo in the service', function(done) {

     ///I'm getting this error here: Error: Trying to stub property 'foo' of undefined
     sinon.stub( something.service , 'foo' )
     .returns(when(function() {
         return ['bar', 'baz'];
     }));


     let promise = something.doSomething({param:'for service', someFlag:true});
});
然后检查doSomething方法是否确实在执行它应该执行的逻辑


我如何为在我的something函数中定义为私有闭包作用域变量的服务注入模拟服务和/或模拟函数

我认为您需要在测试中要求服务,并直接将其存根

let service = require('../../service.js');
sinon.stub(service, 'foo' )
.returns(when(function() {
         return ['bar', 'baz'];
}));
我最终使用了


现在有什么东西可以用吗?尝试是它应该使用被注入的模拟对象。