【发布时间】:2014-02-04 15:09:42
【问题描述】:
当我不限制/去抖动测试通过的功能时。
但是,当我对事件进行去抖动以防止服务器泛滥时,测试不再通过。摩卡输出AssertionError: expected execute to have been called at least once, but it was never called
应该注意,去抖动调用在实时代码中没有错误。这就是为什么我对测试失败的原因感到非常困惑。
测试:
describe('When information is entered into the search fields', function () {
it('vents up the search:for:churches command', function () {
var executeStub = sinon.stub(App, 'execute');
view = new SearchForm()
view.render();
view.$el.find('input[name=church_name]').val('baptist')
view.$el.find('input[name=zip]').val('61615')
view.$el.find('input[name=zip]').trigger($.Event('keypress'))
expect(executeStub).to.have.been.called
view.close();
PEP.execute.restore()
});
});
无节流:
var SearchForm = Backbone.Marionette.ItemView.extend({
template: 'search_form',
events: {
'keypress [data-search-field]' : 'searchForChurches'
},
searchForChurches: function() {
console.log('not debounced')
var searchData = Backbone.Syphon.serialize(this);
App.execute("search:for:churches", searchData);
}
});
节流:
var SearchForm = Backbone.Marionette.ItemView.extend({
template: 'search_form',
events: {
'keypress [data-search-field]' : 'searchForChurches'
},
searchForChurches: _.debounce(function() {
console.log('debounced')
var searchData = Backbone.Syphon.serialize(this);
App.execute("search:for:churches", searchData);
}, 200)
});
编辑: 我也发了一个相关的后续问题:https://stackoverflow.com/questions/21167488/how-to-test-a-debounced-throttled-backbone-view-event-with-mocha-to-ensure-its-a
【问题讨论】:
标签: javascript backbone.js underscore.js mocha.js sinon