javascript - 在 Sinon.js spy 上调用的测试方法

标签 javascript sinon

我正在使用 SinonJS 来测试我的 Controller 是否在我的 View 中调用方法。我想测试是否使用 testSeat 的值调用了 addSeat 方法。我最大的努力是下面的代码,但我收到了一个错误。我希望能够在不将我的 View 模块带入我的测试中的情况下进行测试。这可能吗?

describe('Adding a seat', function() {
  view1Spy = {
        addSeat: sinon.spy(),
        render: sinon.spy()
  };
  beforeEach(function() {
    newSeat = seatingChartController.addSeat(3, 4);
    seatingChartController.registerView(view1Spy);
  });
  it('Calls addSeat on each view with the added seat', function() {
    expect(view1Spy.addSeat).to.be.calledWith(newSeat);
  });
}

编辑:

这是错误:

1) Seating Chart Controller Adding a seat Calls addSeat on each view with the added seat
     Failure/Error: 'undefined' is not a function (evaluating 'expect(view1Spy.addSeat).to.be.calledWith(newSeat)'

最佳答案

看起来在这种情况下你会想要这样的东西:

view1Spy = {addSeat: sinon.spy()};
// ...
expect(view1Spy.addSeat.calledWith(newSeat)).to.be.true;

关于javascript - 在 Sinon.js spy 上调用的测试方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29566600/

相关文章:

javascript - jQuery轮播下一个元素不滑入

javascript - 如何正确改变状态? (只读错误)

javascript - 在测试 JavaScript 时,有什么比 setTimeout 更好的等待异步回调的方法吗?

javascript - 在 sinon 中模拟 HTTP 获取

javascript - 当屏幕CSS像素比高于1时,在不同浏览器上获得相同的window.screen.height和window.screen.width

javascript - 在不使用 Raphael javascript 库中的 boundingBox 函数的情况下获取文本的大小

javascript - Sinon 监视继承函数

javascript - 在 Node.js 中监视 require

Sinon 创建 stub 实例 - 包括 protected 属性

javascript - meteor JS : how to change localhost to custom domain?