admin管理员组文章数量:1334322
I'm trying to stub a function with nodeunit in a Node.js app. Here's a simplified version of what I'm trying to do:
In lib/file.js
:
var request = require('request');
var myFunc = function(input, callback){
request(input, function(err, body){
callback(body);
});
};
In test/test.file.js
:
var file = require('../lib/file');
exports['test myFunc'] = function (test) {
request = function(options, callback){
callback('testbody');
};
file.myFunc('something', function(err, body){
test.equal(body, 'testbody');
test.done();
});
};
It seems like I'm not overriding request
properly, because when I try to run the test, the actual non-stub request
is getting called, but I can't figure out what the correct way to do it is.
EDIT:
To expand on Ilya's answer below, with my example above.
in lib/file/js
:
module.exports = function(requestParam){
return {
myFunc: function(input, callback){
requestParam(input, function(err, body){
callback(body);
});
}
}
}
Then in test/test.file.js
:
var fakeRequestFunc = function(input, callback){
// fake request function
}
var file = require('../lib/file')(fakeRequestFunc)(
//test stuff
}
I'm trying to stub a function with nodeunit in a Node.js app. Here's a simplified version of what I'm trying to do:
In lib/file.js
:
var request = require('request');
var myFunc = function(input, callback){
request(input, function(err, body){
callback(body);
});
};
In test/test.file.js
:
var file = require('../lib/file');
exports['test myFunc'] = function (test) {
request = function(options, callback){
callback('testbody');
};
file.myFunc('something', function(err, body){
test.equal(body, 'testbody');
test.done();
});
};
It seems like I'm not overriding request
properly, because when I try to run the test, the actual non-stub request
is getting called, but I can't figure out what the correct way to do it is.
EDIT:
To expand on Ilya's answer below, with my example above.
in lib/file/js
:
module.exports = function(requestParam){
return {
myFunc: function(input, callback){
requestParam(input, function(err, body){
callback(body);
});
}
}
}
Then in test/test.file.js
:
var fakeRequestFunc = function(input, callback){
// fake request function
}
var file = require('../lib/file')(fakeRequestFunc)(
//test stuff
}
Share
Improve this question
edited Oct 21, 2012 at 22:03
Joe
asked Oct 19, 2012 at 5:55
JoeJoe
2903 silver badges9 bronze badges
1
- Does this answer your question? nodejs override a function in a module – leoschet Commented Jan 22, 2020 at 16:38
2 Answers
Reset to default 3As you noticed, variables declared in one module, cannot easily be accessed from another module. In such cases, you have two mon variants:
1) Declare everything you need in every module (not your case, I suppose)
2) Pass parameters to a function
var ab = "foo",
index = require('/routes/index')(ab);
When you call a function form a module, you may pass it 'request' or any other vars or object you need.
I've run into similar issue. After exploring request
module code my solution was using request.get
instead of request
in my code (do exactly the same). And then stub it in test like that: https://github./anatoliychakkaev/resizer-app/blob/master/test/resizer.js#L25
It also possible to stub result of 'require' method in nodejs. Check sources on lib/module.js to manage how to do it. It should be something like:
require('module')._cache['/path/to/request.js'] = your_stub
But I don't like this solution because it doesn't work in 100% of cases and may stop working in future versions of node (this is not public api), so you should use this way only in case when it's not possible to use other ways of stubbing.
本文标签: javascriptOverriding functions in other modules in nodejsStack Overflow
版权声明:本文标题:javascript - Overriding functions in other modules in node.js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742355687a2459360.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论