最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - sinon stub not replacing function - Stack Overflow

programmeradmin4浏览0评论

I'm trying to use sinon stub to replace a function that might take along time. But when I run the tests, the test code doesn't seem to be using the sinon stubs.

Here is the code I'm trying to test.

function takeTooLong() {
    return  returnSomething();
}

function returnSomething() {
    return new Promise((resolve) => {
        setTimeout(() => {
          resolve('ok')
        }, 1500)
    })
}

module.exports = {
  takeTooLong,
  returnSomething
}

and this is the test code.

const chai = require('chai')
chai.use(require('chai-string'))
chai.use(require('chai-as-promised'))
const expect = chai.expect
chai.should()
const db = require('./database')
const sinon = require('sinon')
require('sinon-as-promised')

describe('Mock the DB connection', function () {

it('should use stubs for db connection for takeTooLong', function (done) {

    const stubbed = sinon.stub(db, 'returnSomething').returns(new Promise((res) => res('kk')));
    const result = db.takeTooLong()

    result.then((res) => {

        expect(res).to.equal('kk')
        sinon.assert.calledOnce(stubbed);
        stubbed.restore()
        done()
    }).catch((err) => done(err))

})

I get an assertion error

 AssertionError: expected 'ok' to equal 'kk'
      + expected - actual

  -ok
  +kk

What am I doing wrong? Why isn't the stub being used ? The test framework in Mocha.

I'm trying to use sinon stub to replace a function that might take along time. But when I run the tests, the test code doesn't seem to be using the sinon stubs.

Here is the code I'm trying to test.

function takeTooLong() {
    return  returnSomething();
}

function returnSomething() {
    return new Promise((resolve) => {
        setTimeout(() => {
          resolve('ok')
        }, 1500)
    })
}

module.exports = {
  takeTooLong,
  returnSomething
}

and this is the test code.

const chai = require('chai')
chai.use(require('chai-string'))
chai.use(require('chai-as-promised'))
const expect = chai.expect
chai.should()
const db = require('./database')
const sinon = require('sinon')
require('sinon-as-promised')

describe('Mock the DB connection', function () {

it('should use stubs for db connection for takeTooLong', function (done) {

    const stubbed = sinon.stub(db, 'returnSomething').returns(new Promise((res) => res('kk')));
    const result = db.takeTooLong()

    result.then((res) => {

        expect(res).to.equal('kk')
        sinon.assert.calledOnce(stubbed);
        stubbed.restore()
        done()
    }).catch((err) => done(err))

})

I get an assertion error

 AssertionError: expected 'ok' to equal 'kk'
      + expected - actual

  -ok
  +kk

What am I doing wrong? Why isn't the stub being used ? The test framework in Mocha.

Share Improve this question edited Nov 25, 2016 at 12:54 AshanPerera asked Nov 25, 2016 at 12:41 AshanPereraAshanPerera 6281 gold badge8 silver badges18 bronze badges 1
  • Can you add your export and require methods in the code that you are testing and the test code? – Ivan Drinchev Commented Nov 25, 2016 at 12:47
Add a comment  | 

2 Answers 2

Reset to default 18

Sinon stubs the property of the object, not the function itself.

In your case you are exporting that function within an object.

module.exports = {
  takeTooLong,
  returnSomething
}

So in order to properly call the function from the object, you need to replace your function call with the reference to the export object like :

function takeTooLong() {
    return module.exports.returnSomething();
}

Of course based on your code, you can always refactor it :

var exports = module.exports = {

    takeTooLong: function() { return exports.returnSomething() }

    returnSomething: function() { /* .. */ }

}

You might want to have a look at Proxyquire to stub/spy directly exported functions. https://www.npmjs.com/package/proxyquire/

发布评论

评论列表(0)

  1. 暂无评论