I tried a dummy module and to stub it, but does not work.
the app.js
function foo()
{
return run_func()
}
function run_func()
{
return '1'
}
exports._test = {foo: foo, run_func: run_func}
the 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)
})
})
I tried the advice from: sinon stub not replacing function
But still the same. It does not replace the function.
You have a couple of problems here. The first is that you're calling
test_stub.restore()
immediately after creating the stub, which causes it to replace itself with the original function, effectively undoing the stub completely.restore
is meant for cleaning up fake methods after your test is done. So you do want to call it, but you should do so in anafterEach.
Your second problem is a little more subtle. Sinon works by overwriting a reference to a function on an object, making it point at something else (in this case, the stub). It can't replace references to the same function in other contexts.
When you call
sinon.stub(app, 'run_func')
, it's a bit like this:... except that the former way stores the original value and name of
app.run_func
, allowing you to easily restore it later.Note that at this point, the variable
app
points to the same object you exported withexports._test = {foo: foo, run_func: run_func}
Yourfoo
function, however, is not referencingrun_func
through this object. It's referencing it directly in the scope ofapp.js
, which sinon cannot affect.Take a look at the following example. You'll also note I cleaned up a few other things:
app.js:
test.js:
Note how
exports
inapp.js
refers to the exact same object thatapp
does intest.js
. This is because modules in node export an empty object by default, which you can assign onto by way of theexports
variable.