Stub模块函数从同一模块调用

时间:2021-09-18 20:58:40

I can't figure out a way to stub a function called from within the same module this function is defined (the stub does not seem to work). Here's an example:

我无法想出一种方法来存储一个函数,该函数在同一模块中调用此函数定义(存根似乎不起作用)。这是一个例子:

myModule.js:

'use strict'

function foo () {
  return 'foo'
}

exports.foo = foo

function bar () {
  return foo()
}

exports.bar = bar

myModule.test.js:

'use strict'

const chai = require('chai')
const sinon = require('sinon')

chai.should()

const myModule = require('./myModule')

describe('myModule', () => {
  describe('bar', () => {
    it('should return foo', () => {
      myModule.bar().should.equal('foo') // succeeds
    })

    describe('when stubbed', () => {
      before(() => {
        sinon.stub(myModule, 'foo').returns('foo2') // this stub seems ignored
      })

      it('should return foo2', () => {
        myModule.bar().should.equal('foo2') // fails
      })
    })
  })
})

This reminds me of Java static functions which are not stubbable (almost).

这让我想起了Java静态函数,这些函数不是几乎不可能的。

Any idea how to achieve what I'm trying to do? I know that extracting foo in a different module will work, but that's not what I'm trying to do here. I'm also aware that invoking foo in the bar method with the keyword this will also work, I'm puzzled toward the use of ̀this in this context (since I'm not using OOP).

知道如何实现我想要做的事情吗?我知道在不同的模块中提取foo会起作用,但这不是我在这里要做的。我也知道在bar方法中使用关键字this调用foo也会起作用,我很困惑在这种情况下使用它(因为我没有使用OOP)。

1 个解决方案

#1


7  

I just tested this. And it works like charm.

我刚试过这个。它就像魅力一样。

'use strict'

function foo () {
  return 'foo';
}

exports.foo = foo;

function bar () {
  return exports.foo();
}

exports.bar = bar;

Explanation

when you do sinon.stub(myModule, 'foo').returns('foo2') then sinon stubs the exported object's foo not the actually foo function from inside your myModule.js ... as you must know, foo is in accessible from outside the module. So when you set exports.foo, the exported object exports.foo stores the ref of foo. and when you call sinon.stub(myModule, 'foo').returns('foo2'), sinon will stub exports.foo and not the actual foo

当你做sinon.stub(myModule,'foo')。return('foo2')然后sinon将导出的对象的foo与myModule.js中的实际foo函数存根...你必须知道,foo是可访问的来自模块外部。因此,当您设置exports.foo时,导出的对象exports.foo会存储foo的ref。当你调用sinon.stub(myModule,'foo')。返回('foo2')时,sinon将存根exports.foo而不是实际的foo

Hope this makes sense!

希望这是有道理的!

#1


7  

I just tested this. And it works like charm.

我刚试过这个。它就像魅力一样。

'use strict'

function foo () {
  return 'foo';
}

exports.foo = foo;

function bar () {
  return exports.foo();
}

exports.bar = bar;

Explanation

when you do sinon.stub(myModule, 'foo').returns('foo2') then sinon stubs the exported object's foo not the actually foo function from inside your myModule.js ... as you must know, foo is in accessible from outside the module. So when you set exports.foo, the exported object exports.foo stores the ref of foo. and when you call sinon.stub(myModule, 'foo').returns('foo2'), sinon will stub exports.foo and not the actual foo

当你做sinon.stub(myModule,'foo')。return('foo2')然后sinon将导出的对象的foo与myModule.js中的实际foo函数存根...你必须知道,foo是可访问的来自模块外部。因此,当您设置exports.foo时,导出的对象exports.foo会存储foo的ref。当你调用sinon.stub(myModule,'foo')。返回('foo2')时,sinon将存根exports.foo而不是实际的foo

Hope this makes sense!

希望这是有道理的!