當我不限制/去除測試通過的功能時。無法測試去抖動的主幹視圖事件
但是,當我爲了防止服務器氾濫而拒絕該事件時,測試不再通過。摩卡輸出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
這應該是公認的答案! – adrichman