2016-03-25 3 views
0

私はブラウザ上で実行中のmochaでチャイのスパイを使用しようとしていますが、何かが動作していません。ブラウザでチャイのスパイが動作しない

<html> 
<head> 
    <meta charset="utf-8"> 
    <title>Mocha Tests</title> 
    <link rel="stylesheet" href="./node_modules/mocha/mocha.css" /> 
</head> 
<body> 
    <div id="mocha"></div> 
    <script src="./node_modules/mocha/mocha.js"></script> 
    <script src="./node_modules/chai/chai.js"></script> 
    <script src="./node_modules/chai-spies/chai-spies.js"></script> 

    <script> 
    mocha.setup('bdd') 
    mocha.reporter('html'); 
    var expect = chai.expect; 
    </script> 

    <script src="./test.js"></script> 

    <script> 
    if (navigator.userAgent.indexOf('PhantomJS') < 0) { 
     mocha.run(); 
    } 
    </script> 
</body> 
</html> 

そして、このテストファイル:このHTML構成で

describe("test spies", function() { 
    it("are worning", function() { 
     function myFunction() { 
      console.log("Hello"); 
     } 
     var spy = chai.spy(myFunction); 
     expect(spy).to.be.spy; 
     myFunction(); 
     expect(spy).to.have.been.called(); 
    }); 
}); 

私は、次のアサーションエラーを取得:

AssertionError: expected { Spy } to have been called 
    at Context.<anonymous> (test/input-text-test-fixture.html:282:28) 

誰かが私は何を私に伝えることができます間違っている?

+0

どのブラウザをお使いですか? –

+0

私はクロムに取り組んでいます – Marchinka

+0

奇妙な、テストコードはOKであるようです。 –

答えて

0

機能ではなく、スパイ自体を呼び出す必要があります。

describe("test spies", function() { 
it("are worning", function() { 
    function myFunction() { 
     console.log("Hello"); 
    } 
    var spy = chai.spy(myFunction); 
    expect(spy).to.be.spy; 
    spy(); // Call the spy here 
    expect(spy).to.have.been.called(); 
}); 

});

スパイは関数をラップし、内部的に呼び出します。関数自体を呼び出すだけで、関数が実際に呼び出されたことを記録する機会はありません。chai-spies

+0

ありがとう!それが問題でした。 – Marchinka

関連する問題