调用 Sinon.js 间谍的测试方法
Testing method called on Sinon.js spy
我正在使用 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)'
在这种情况下,您可能需要这样的东西:
view1Spy = {addSeat: sinon.spy()};
// ...
expect(view1Spy.addSeat.calledWith(newSeat)).to.be.true;
我正在使用 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)'
在这种情况下,您可能需要这样的东西:
view1Spy = {addSeat: sinon.spy()};
// ...
expect(view1Spy.addSeat.calledWith(newSeat)).to.be.true;