在Sinon.js间谍上调用测试方法

时间:2015-04-10 16:42:36

标签: javascript sinon

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

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)'

1 个答案:

答案 0 :(得分:3)

看起来在这种情况下你需要类似的东西:

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