sinon stub not replacing function.

痴心易碎 提交于 2019-12-21 09:22:10

问题


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.


回答1:


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 an afterEach.

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:

app.run_func = sinon.stub()

... 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 with exports._test = {foo: foo, run_func: run_func} Your foo function, however, is not referencing run_func through this object. It's referencing it directly in the scope of app.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:

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');
        });
    });
});

Note how exports in app.js refers to the exact same object that app does in test.js. This is because modules in node export an empty object by default, which you can assign onto by way of the exports variable.



来源:https://stackoverflow.com/questions/48931815/sinon-stub-not-replacing-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!