使用Jasmine对javascript/jQuery事件进行单元测试
Unit-testing javascript/jQuery events with Jasmine
我有这个代码:
$(document).on('click', '#clear-button', clearCalculatedPrice)
clearCalculatedPrice = ->
$('#price_rule').removeAttr('data-original-title')
$('#calculated-price').removeAttr('data-original-title')
$('#calculated-price').empty()
这样的测试对它
describe 'Call taker', ->
it 'should clear calculated price', ->
spyOn($.fn, 'removeAttr')
spyOn($.fn, 'empty')
clearCalculatedPrice()
expect($.fn.removeAttr).toHaveBeenCalled()
expect($.fn.empty).toHaveBeenCalled()
我需要创建另一个测试,它将检查当事件被触发时是否调用了函数,比如这个
it 'should clear calculated price when "click" event is triggered', ->
$('#destination-clear-button').trigger('click')
spyOn($.fn, 'removeAttr')
spyOn($.fn, 'empty')
expect($.fn.removeAttr).toHaveBeenCalled()
expect($.fn.empty).toHaveBeenCalled()
但这个测试并不像我想的那样有效。所以问题是:测试事件处理程序的单元测试应该如何编写
订单调用可能是错误的
it 'should clear calculated price when "click" event is triggered', ->
# set spys
spyOn($.fn, 'removeAttr')
spyOn($.fn, 'empty')
# trigger
$('#destination-clear-button').trigger('click')
# check
expect($.fn.removeAttr).toHaveBeenCalled()
expect($.fn.empty).toHaveBeenCalled()
相关文章:
- 我的单元测试选项是什么
- 如何在Angular单元测试中从另一个控制器的rootScope将方法添加到rootScope中
- 如何对CSS transitionEnd事件后执行的JavaScript进行单元测试
- 单元测试:使用酶模拟父组件中子组件的点击事件
- AngularJS:指令限制:“E”阻止在Jasmine单元测试中调用元素click()事件
- 如何在angularjs单元测试中触发mousemove事件
- 触发单元测试中的键控事件
- 是否应该对 JavaScript 事件处理程序进行单元测试
- 使用Jasmine对javascript/jQuery事件进行单元测试
- 我可以在茉莉花单元测试中伪造存储事件吗
- 使用jasmin对AngularJS$事件进行单元测试
- 如何最好地对事件抽象进行单元测试
- 在Nodejs中对事件进行单元测试的最佳方法是什么?
- 如何在Jasmine中为进入事件指令编写单元测试
- 如何在backbone.js中为事件编写单元测试
- 在IE中单元测试事件处理程序
- 为事件创建单元测试
- 如何在Angular指令的Jasmine单元测试中触发一个click事件?
- 触发单元测试的事件
- 单元测试angularjs指令监听事件