【发布时间】:2012-02-29 19:32:41
【问题描述】:
我不知道如何为我的 JS 运行这个 Jasmine 测试,当然其他人也有这个问题。也许我做错了,或者不可能 - 我没有找到任何暗示。问题与以下事实有关 - 在 jQuery 中 - $(this) 与例如选择的元素不同。 $("#this-id"):
Javascript:
[..]
$("#button-id").on("click", function(e) { callSomeFunctionWith( $(this) ); } );
Jasmine-Test (CoffeeScript):
[..]
spyOn some.object, "callSomeFunctionWith"
spyOnEvent( $("#button-id"), 'click' )
$("#button-id").trigger( "click" )
expect( some.object.callSomeFunctionWith ).toHaveBeenCalledWith( $("#button-id") )
不幸的是,这个测试失败了(有任何变化,比如在我的 Jasmine 测试中首先将 ref 存储到一个变量中),因为该函数不是用 $("#button-id") 调用的,而是用 $( this) 和 $(this) != $("#button-id")。
谁能告诉我如何完成这个测试?我很迷茫。甚至Remy Sharp's great article on jQuery and $(this) 也没有让我更进一步。
【问题讨论】:
标签: jquery testing jasmine jasmine-jquery