ジャスミンを使用してクリックイベントの後に ajax 呼び出しをテストしようとしています。クリックを認識させるためにさまざまなことを試しましたが、ajaxを起動することはできません。
ここに私のテストファイルがあります:
# use require to load any .js file available to the asset pipeline
#= require jquery
#= require jquery_ujs
#= require jquery.ui.all
#= require jquery.scrollTo.min
#= require application/save_search
setUpHTMLFixture = ->
loadFixtures "home_page"
describe "save_search", ->
beforeEach ->
setUpHTMLFixture()
it "should get the list of friends and insert it in the page", ->
print $("#save-search")
spyOn jQuery, "ajax"
$("#save-search").click()
print $("#save-search")
jQuery.ajax.mostRecentCall.args[0].success "Something"
expect($("#save-search").length).toBe 2
私のコード:
jQuery ->
$("#save-search").click ->
draggables = undefined
draggables = {}
jQuery.each jQuery(".ui-draggable"), (index, draggable) ->
child = undefined
parentId = undefined
classes = undefined
left = undefined
offset = undefined
top = undefined
value = undefined
draggable = jQuery(this)
parentId = draggable.parent().attr("id")
child = draggable.children()
value = child.val()
classes = draggable.attr("class").replace(/ui-draggable|down|cloner|\s/g, "")
key = classes + index
offset = draggable.offset()
left = offset.left
top = offset.top
draggables[key] =
value: value
left: left
top: top
parent_id: parentId
draggables["name"] = $("#search-name").val()
$.ajax
type: "POST"
url: "/searches/save_search"
data:
draggables: JSON.stringify(draggables)
dataType: "json"
success: (msg) ->
alert "Data Saved: " + msg
false
私の備品:
<a id="save-search">Save Search</a>
<div class="ui-draggable">some stuff</div>
<div class="ui-draggable">some stuff</div>