したがって、ジャスミンテストを構築しようとしているこのJSプロトタイプ(クラス)があり、これらのテストを機能させる方法を理解できないようです。
クラスの重要な部分は次のとおりです。
class Calendar extends BasicView
initialize: (options) ->
this.$el = options.el
{@sidebar} = options
this.$('.select-day').click this.display_date
this
display_date: (e) =>
console.log 'display_date called' # <~~ this is printing
... do stuff ...
そして私が書いているテスト:
describe "Calendar", ->
calendar = null
beforeEach ->
loadFixtures "calendar/calendar.html"
describe "#initialize", ->
beforeEach ->
calendar = new Calendar().initialize
el: $('.event-calendar')
# just mocking dependency class
sidebar: jasmine.createSpyObj(CurrentlyViewing, ["$"])
it "listens for click event on .select-day", ->
spyOn(calendar, 'display_date')
calendar.$('.select-day:eq(1)').trigger 'click'
expect(calendar.display_date).toHaveBeenCalled()
テストを実行するExpected spy display_date to have been called.
と、実際のメソッドが呼び出されているにもかかわらず取得されます。私がスパイしているのはCalendar
、私が初期化したインスタンスではないということはわかっていますが、その方法や理由はわかりません。
誰でも私にできる助けをいただければ幸いです。