Unit testing 同步事件的主干单元测试策略?
My model在每次同步后设置startAttributes:Unit testing 同步事件的主干单元测试策略?,unit-testing,backbone.js,mocha.js,sinon,chai,Unit Testing,Backbone.js,Mocha.js,Sinon,Chai,My model在每次同步后设置startAttributes: this.on('sync', function(model) { model.startAttributes = _.clone(model.attributes); }); 如何继续测试是否正确执行了此操作 describe('History', function() { beforeEach(function() { this.address = new app.mo
this.on('sync', function(model) {
model.startAttributes = _.clone(model.attributes);
});
如何继续测试是否正确执行了此操作
describe('History', function() {
beforeEach(function() {
this.address = new app.models.Address();
this.sync_stub = sinon.stub(this.address, 'sync');
});
it("should set the startAttributes when the model syncs", function () {
this.address.save();
should.exist(this.startAttributes);
});
afterEach(function() {
this.sync_stub.restore();
});
});
我不能在不存根的情况下调用save,因为它会导致错误,但如果我存根它,则永远不会触发同步事件。如果我对同步方法进行存根,同样的情况也适用。您可以手动触发类似以下模型的同步事件:
model.trigger('sync');
这是一个好主意,但我不知道这是否是一件好事,因为我不测试save是否触发事件。