我尝试了一个虚拟模块并将其存根,但不起作用。
app.js
function foo()
{
return run_func()
}
function run_func()
{
return '1'
}
exports._test = {foo: foo, run_func: run_func}
test.js
app = require("./app.js")._test
describe('test', function(){
it('test', function(){
var test_stub = sinon.stub(app, 'run_func').callsFake(
function(){
return '0'
})
test_stub.restore()
var res = app.foo()
assert.equal('0', res)
})
})
我尝试了以下建议: sinon stub not replacing function
但仍然如此。它不会取代该功能。
答案 0 :(得分:8)
这里有几个问题。第一个是你在创建存根之后立即调用test_stub.restore()
,这会导致它用原始函数替换它自己,有效地完全撤消存根。
restore
用于在测试完成后清理假方法。所以你确实想要调用它,但你应该在afterEach.
你的第二个问题有点微妙。 Sinon通过覆盖对对象上的函数的引用来工作,使其指向其他东西(在这种情况下,存根)。它不能替换在其他上下文中对相同函数的引用。
当你致电sinon.stub(app, 'run_func')
时,它有点像这样:
app.run_func = sinon.stub()
...除了前一种方式存储app.run_func
的原始值和名称,以便您以后轻松恢复。
请注意,此时,变量app
指向您使用exports._test = {foo: foo, run_func: run_func}
导出的同一对象。但是,foo
函数未通过此对象引用run_func
。它直接在app.js
的范围内引用它,而sinon不会影响它。
看一下下面的例子。你还会注意到我清理了其他一些东西:
app.js:
exports.foo = function() {
return exports.run_func();
};
exports.run_func = function() {
return '1';
};
test.js:
const app = require('./app');
const sinon = require('sinon');
describe('app', function() {
describe('foo', function() {
beforeEach(function() {
sinon.stub(app, 'run_func').returns('0');
});
afterEach(function() {
app.run_func.restore();
});
it('returns result of app.run_func', function() {
assert.equal(app.foo(), '0');
});
});
});
请注意exports
中的app.js
如何引用app
与test.js
完全相同的对象。这是因为默认情况下节点中的模块会导出一个空对象,您可以通过exports
变量将其分配到该对象。