2012-08-21 19 views
18

私はこのモジュール(2)のテスト(1)を実装しようとしています。
私の目的は、特定のイベントが発生したときにコレクションが取得されるかどうかをチェックすることです。
(2)の私のコメントから分かるように、私はメッセージを受け取りますError: Expected a spy, but got Function.
モジュールは動作しますが、テストは失敗します。何か案は?スパイが必要ですが、機能があります。


(1)

// jasmine test module 

describe('When onGivePoints is fired', function() { 
    beforeEach(function() { 
     spyOn(this.view.collection, 'restartPolling').andCallThrough(); 
     app.vent.trigger('onGivePoints'); 
    }); 
    it('the board collection should be fetched', function() { 
     expect(this.view.collection.restartPolling).toHaveBeenCalled(); 
     // Error: Expected a spy, but got Function. 
    }); 
}); 

(2)

// model view module 
return Marionette.CompositeView.extend({ 
    initialize: function() { 
     this.collection = new UserBoardCollection(); 
     this.collection.startPolling(); 
     app.vent.on('onGivePoints', this.collection.restartPolling); 
    }, 
    // other code 
}); 
+0

何が起こっているかを見るのに十分なコードがありません。関数が属するオブジェクト定義と、少なくともオブジェクトをインスタンス化するコードを含めて、個々の関数以上のものを含めてください。 –

+0

@DerickBaileyお時間をいただきありがとうございます。モードコードで質問を更新しました。 –

+0

私はJasmineではなくQUnitを使用していますが、app.vent.triggerをbeforeEachではなくitメソッドに呼び出すべきですか? – codemonkey

答えて

32

あなたは、この場合にはプロトタイプであり、実際の方法、に取得する必要があります。

describe('When onGivePoints is fired', function() { 
    beforeEach(function() { 
     spyOn(UsersBoardCollection.prototype, 'restartPolling').andCallThrough(); 
     app.vent.trigger('onGivePoints'); 
    }); 
    it('the board collection should be fetched', function() { 
     expect(UsersBoardCollection.prototype.restartPolling).toHaveBeenCalled(); 
    }); 
}); 

プロトタイプをスパイすることは、スパイしたい実際のインスタンスにアクセスできない場合に使用できる素晴らしいトリックです。

2

同じ問題が発生しましたが、関数呼び出しで引数を渡して解決しました。そして、あなたは私がロードされsinonの2つのバージョンを持っていた、または多分私が正しくsinon-ジャスミンを初期化されなかったので、私はこのバグを持っていたit

var data = {name:"test"} 
spyOn(UsersBoardCollection.prototype, "restartPolling").and.callThrough(); 
UsersBoardCollection.prototype.restartPolling(data); 
expect(UsersBoardCollection.prototype.restartPolling).toHaveBeenCalled(); 
+0

ありがとうございました。 –

-2

にこのようなテストケースを記述する必要があります。仕様設定でsinonとsinon jasmineを明示的に読み込むと、正しく動作するようになりました。

関連する問題