当我不对该函数进行节流/去抖动时,测试通过。
然而,当我对事件进行去抖动以防止服务器被淹没时,测试不再通过。Mocha输出了
Un-throttled: 没有限速
被限速:
编辑:我也发布了一个相关的后续问题:https://stackoverflow.com/questions/21167488/how-to-test-a-debounced-throttled-backbone-view-event-with-mocha-to-ensure-its-a
然而,当我对事件进行去抖动以防止服务器被淹没时,测试不再通过。Mocha输出了
AssertionError: 预期execute至少被调用一次,但它从未被调用
应该注意的是,去抖动调用在实际代码中没有错误。这就是为什么我非常困惑为什么测试失败的原因。
测试:
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()
});
});
Un-throttled: 没有限速
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