Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unit testing 同步事件的主干单元测试策略?_Unit Testing_Backbone.js_Mocha.js_Sinon_Chai - Fatal编程技术网

Unit testing 同步事件的主干单元测试策略?

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

My model在每次同步后设置startAttributes:

    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是否触发事件。