$( "#要素-ID")のJasmineを使用してjuseryをテストする方法( "#element-id")

StackOverflow https://stackoverflow.com/questions/9505437

質問

私のJSのジャスミンテストを実行する方法を知りません。また、他のPPLにもこの問題があります。たぶん私は間違っているかどうか、それは不可能です - 私はこれにヒントを見つけませんでした。問題は、jQuery - $(これ)では、例えば選択された要素と同じではないという事実と関係があります。$( "#this-id"):

JavaScript:

[..]
$("#button-id").on("click", function(e) { callSomeFunctionWith( $(this) ); } );
.

ジャスミン検定(CoffeeScript):

[..]
spyOn some.object, "callSomeFunctionWith"
spyOnEvent( $("#button-id"), 'click' )

$("#button-id").trigger( "click" )
expect( some.object.callSomeFunctionWith ).toHaveBeenCalledWith( $("#button-id") )
.

残念な場合、このテストは失敗しました(私のジャスミンテストで最初にREFを変数に格納するようなバリエーションで)、関数は$( "#button-id")と呼ばれませんが、代わりに$(この)、および$(これ)!= $( "#ボタンID")

誰かがこのテストを達成する方法を教えてください。私は全く失われています。 Remy Sharpの素晴らしい記事についてjQueryと$(この)

役に立ちましたか?

解決

Ok, now I've got the solution to my problem. The solution is easy, the explanation not. I'll explain the solution from scratch.

This is my Javascript code with jQuery that I want to test using jasmine-jquery:

$( "input.toggler" ).on( "click", function( e ) {
  [...]
  doSomethingWith( $(this) );
} );

And now using Jasmine-jQuery I want to ensure that the JS function "doSomethingWith" gets called with the correct "$(this)".

First one might think that $(this) === $( "input.toggler" ), but that is not true. Inside the callback function of the click handler, the $(this) jQuery uses is neither the jQuery object $( "input.toggler" ) nor the DOM element referenced by that object. As Remy Sharp explains in his really nice article "jQuery's this: demystified", the "this" inside the callback function is the DOM element, but $(this) creates a jQuery object from that DOM element. And that is not identical to the jQuery object $( "input.toggler" ).

So if you want to test this with Jasmine using the function "toHaveBeenCalledWith", you have to first extract the DOM element using either document.getElementById(...) or else document.getElementsByTagName(...)[INDEX] (where INDEX is the index of the element you want, since the latter function gives you an array of DOM elements), which is plain old Javascript. Then, when you have extracted the DOM element wanted, you have to create a jQuery-object from it by enclosing it in $( and ).

My passing Jasmine-jQuery-test finally looks something like this (using Coffeescript):

it "does something with my input element", ->
  DOM_input_element = document.getElementsByTagName( "input" )[0] # Choose the correct DOM element here

  spyOn myobject.functions, "doSomethingWith"
  spyOnEvent( $( 'input.toggler' ), 'click' )

  [...]

  $( 'input.toggler' ).trigger( 'click' )

  # Check for changes after click:
  expect( myobject.functions.doSomethingWith ).toHaveBeenCalledWith( $( DOM_input_element ) )

So the "$(this)" from my Javascript code translates to "$(DOM_input_element)" in my Jasmine-jQuery test.

Hopefully this helps you with your projects! It took me quite a while to figure this out.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top