【问题标题】:sinon spy on function not workingsinon 间谍功能不起作用
【发布时间】:2018-12-26 17:16:21
【问题描述】:

我正在尝试为这个简单的中间件函数编写一个独立的测试

function onlyInternal (req, res, next) {
  if (!ReqHelpers.isInternal(req)) {
    return res.status(HttpStatus.FORBIDDEN).send()
  }
  next()
}

// Expose the middleware functions
module.exports = {
  onlyInternal
}

这不起作用

describe('success', () => {

  let req = {
    get: () => {return 'x-ciitizen-token'}
  }
  let res = {
    status: () => {
      return {
        send: () => {}
      }
    }
  }
  function next() {}
  let spy

  before(() => {
    spy = sinon.spy(next)
  })

  after(() => {
    sinon.restore()
  })

  it('should call next', () => {
    const result = middleware.onlyInternal(req, res, next)
    expect(spy.called).to.be.true <-- SPY.CALLED IS ALWAYS FALSE EVEN IF I LOG IN THE NEXT FUNCTION SO I KNOW IT'S GETTING CALLED
  })
})

但确实如此..

describe('success', () => {

  let req = {
    get: () => {return 'x-ciitizen-token'}
  }
  let res = {
    status: () => {
      return {
        send: () => {}
      }
    }
  }
  let next = {
    next: () => {}
  }
  let spy

  before(() => {
    spy = sinon.spy(next, 'next')
  })

  after(() => {
    sinon.restore()
  })

  it('should call next', () => {
    const result = middleware.onlyInternal(req, res, next.next)
    expect(spy.called).to.be.true
  })
})

为什么不能只监视函数工作?

【问题讨论】:

    标签: javascript unit-testing mocha.js sinon chai


    【解决方案1】:

    Sinon 无法更改现有函数的内容,因此它创建的所有间谍都只是现有函数的包装器,用于计算调用次数、记忆参数等。

    所以,你的第一个例子是这样的:

    function next() {}
    let spy = sinon.spy(next);
    next(); // assuming that middleware is just calling next
    // spy is not used!
    

    你的第二个例子,等于:

    let next = { next: () => {} }
    next.next = sinon.spy(next.next); // sinon.spy(obj, 'name') just replaces obj.name with spy on it
    next.next(); // you actually call spy which in in turn calls original next.next
    //spy is called. YaY
    

    因此,在 sinon 中“spying”和“stubbing”的关键在于您必须在测试中使用 spied/stubbed 函数。您的原始代码只是使用了原始的、非间谍功能。

    【讨论】:

    • 又名在我的第一个示例中,我需要这样做middleware.onlyInternal(req, res, spy)
    猜你喜欢
    • 1970-01-01
    • 2018-08-16
    • 2016-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多