2011-07-22 19 views
1

私はジャスミンと単体テストの方法を学びたいと思っています。 jqueryのクリックイベントが発生したときにメソッドが呼び出されることをテストしようとしている、非常に簡単な例を作成しました。私はこれを働かせるように見えない。誰かが助けてくれますか?どうもありがとう!!jasmine単体テストが単純なjqueryで動作しないclick

私は次のエラーを取得:

ReferenceError: Butthead is not defined in http://localhost:4243/spec/buttheadSpec.js (line 11) 

spyOn could not find an object to spy upon for responseAlert() 

をここに私のユニットテストコードここ

function Butthead(){ 
} 

Butthead.prototype.responseAlert = function(){ 
    alert('You are a butthead'); 
} 

$(document).ready(function(){ 

    var butthead = new Butthead(); 

    $('#myButton').click(function(){ 

     butthead.responseAlert(); 
    }); 
} 

います

// Test Fixture 
describe('When clicking myButton', function(){ 
    var butthead; 

    // Set up 
    beforeEach(function(){ 
    butthead = new Butthead(); 
    }); 


    // Test 
    it('should say Hello', function(){ 

    spyOn(butthead, 'responseAlert'); 

    $('#myButton').click(); 

    expect(butthead.responseAlert).toHaveBeenCalled(); 
    }); 

}); 

答えて

0

これらは2つの別々のファイルにありますか?私はそれが間抜けな質問のように聞こえることを知っていますが、私はジャスミンで始まったときに私が間違っていました。 "ソース"ファイルと "スペック"ファイルを分離する必要があります。

最初にスペックランナーページの "Butthead"クラス宣言を含むコードファイルを参照することを忘れないでください。また、aだけではなく、スクリプトタグ参照を閉じる必要があります。私は自分の前でこの間違いを犯した。

関連する問題