2017-09-17 5 views
1

私はジャスミンとスパイのことを初めて知りました。正しい方向を指すことができれば幸いです。メソッドを使用せずにspyOn()を使用していますか?

私はユニットテストでカバーしたいイベントリスナーがあります。

var nextTurn = function() { 
    continueButton.addEventListener("click", displayComputerSelection) 
}; 

nextTurn(); 

を一般的な考え方は、「displayComputerSelection」関数をスパイすることです。スパイの基本的な構造は

it ("should call fn displayComputerSelection on continueButton click", function(){ spyOn(displayComputerSelection); continueButton.click(); expect(displayComputerSelection).toHaveBeenCalled();

は私が応答No method name suppliedを得るspyOn(<object>, <methodName>)です。 jasmine.createSpyを試してみましたが、動作させることができませんでした。 私はどのようにして期待される方法を代用しますか?あなたのシナリオでは

答えて

0

あなたの問題

このfuncは、あなたのスパイと交換したいものであるので、全体の質問は、方法や場所でdisplayComputerSelectionが定義されています。

jasmine.createSpy()

それはあなたがしたいjasmine.createSpy()です。たとえば、次の例は、あなたがそれを使う方法の例です - 完全にテストされていません - 意図された言い訳はありません。

var objectToTest = { 
    handler: function(func) { 
    func(); 
    } 
}; 

describe('.handler()', function() { 
    it('should call the passed in function', function() { 
    var func = jasmine.createSpy('someName'); 

    objectToTest.handler(func); 

    expect(func.calls.count()).toBe(1); 
    expect(func).toHaveBeenCalledWith(); 
    }); 
}); 
+0

多くの感謝! 'displayComputerSelection'はグローバル変数なので、私は単に' window'をオブジェクトとして使う必要があることを知りました。 このように動作しました。 'spyOn(window、" displayComputerSelection ");' –

0

私の特定のケースで答えは:

it ("should call displayComputerSelection on continueButton click", function(){ 
    spyOn(window, "displayComputerSelection"); 
    start(); //first create spies, and only then "load" event listeners 
    continueButton.click(); 
    expect(window.displayComputerSelection).toHaveBeenCalled(); 
}); 

ブラウザは、このように、それは時に偵察する、「ウィンドウ」オブジェクトにグローバルVAR /機能をフックするようです。

関連する問題