0

作業中のファイルのコプルがあり、バックボーン ビューにあるこのメソッドでリスナーを作成しようとしています。

appView.js.coffee

namespace "happiness_kpi", (exports) ->
  exports.appView = Backbone.View.extend

    events: 
      "click #happy" : "selection"

    selection: ->
      console.log "selection was called"

index.html.haml

%input{ :type => "image", :src => "/assets/smiley.jpg", :alt => "happy", :id => "happy" }
%input{ :type => "image", :src => "/assets/undecided.jpg", :alt => "undecided", :id => "undecided" }
%input{ :type => "image", :src => "/assets/sad.jpg", :alt => "sad", :id => "sad" }

ここに私の仕様があります:
app_view_spec.js.coffee

it "is called when one of the faces is clicked", ->
  $("body").append('<input alt="happy" id="happy" src="/assets/smiley.jpg" type="image">')
  $("body").append('<input alt="undecided" id="undecided" src="/assets/undecided.jpg" type="image">')
  $("body").append('<input alt="sad" id="sad" src="/assets/sad.jpg" type="image">')
  @subject.selection = sinon.spy()

  $("#happy").click

  sinon.assert.calledOnce(@subject.selection)

「エラー: スパイが 1 回呼び出されると予想されていましたが、0 回呼び出されました」というエラーが表示されます。

前もって感謝します。

4

2 に答える 2

0

そのため、多くの調査と試行錯誤の末、答えにたどり着いたようです。問題は間違いなく、画像を iframe に追加する方法でした。問題は、テストが iframe の外側に新しい要素を作成しているように見え、ページに表示されないことでした。は$('#happy').click()正しく実行されていましたが、iframe に存在しないもので実行されていました。

だから私の解決策:

app_view_spec.js.coffee

beforeEach ->
  $("body").append('<div id="display"></div>')

afterEach ->
  $("body").append('<div id ="display"></div>').remove()

it "is called when one of the faces is clicked", ->
  @subject.emotionSelection = sinon.spy()
  @subject.delegateEvents()

  $("input#happy").click()

  sinon.assert.calledOnce @subject.emotionSelection

appView.js.coffee el: '#display'

initialize: ->
  @render()

events: ->
  'click #happy': @emotionSelection

render: ->
  @$el.html HandlebarsTemplates.faces()

emotionSelection: ->
  console.log "hello"

この方法で行う方がはるかに優れたアプローチであることがわかりました。各画像を個別に追加しようとするのではなく、HAML を iframe に追加しただけです。学んだ教訓。助けてくれてありがとう!

于 2013-07-17T19:01:14.863 に答える