sinon存根不替换功能。 [英] sinon stub not replacing function.

查看:75
本文介绍了sinon存根不替换功能。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了一个虚拟模块并将其存根,但不起作用。



app.js

  function foo()
{
return run_func()
}
function run_func()
{
返回'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不替换功能



但仍然相同。它不会取代函数。

解决方案

这里有几个问题。第一个是你在创建存根之后立即调用 test_stub.restore(),这会导致它用原始函数替换它自己,完全有效地撤消存根。 / p>

restore 用于在测试完成后清理假方法。所以你确实想要打电话,但你应该在之后这样做。



你的第二个问题是更微妙。 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')。return('0');
});

afterEach(function(){
app.run_func.restore();
});

it('返回app.run_func的结果',function(){
assert.equal(app.foo(),'0');
});
});
});

请注意 中的<$ c $> c> app.js 指的是 app test.js 。这是因为节点中的模块默认导出一个空对象,您可以通过 exports 变量将其分配给它。


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

这篇关于sinon存根不替换功能。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆