1

Jasmine で単体テストを行う方法を学ぼうとしています。jqueryクリックイベントがトリガーされたときにメソッドが呼び出されることをテストしようとしている非常に単純な例を作成しました。私はこれを機能させることができないようです。誰か助けてくれませんか?どうもありがとう!!

次のエラーが表示されます。

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();
  });

});
4

1 に答える 1

0

これらは2つの別々のファイルにありますか? ばかげた質問のように聞こえるかもしれませんが、Jasmine を使い始めたときに犯した間違いでした。「ソース」ファイルと「スペック」ファイルを分ける必要があります。

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

于 2011-11-30T07:47:44.620 に答える