2017-10-15 2 views
0

スパイのプロパティを使用しようとしましたが、このエラーで終了します。TypeError:expect.createSpyが関数ではありません

var spy = expect.createSpy(); 
 
spy(); 
 
expect(spy).toHaveBeenCalled();

エラー:

TypeError: expect.createSpy is not a function 
+0

「期待」とは何ですか? – robertklep

+0

ここから見つける:http://chaijs.com/api/bdd/ –

+0

https://github.com/mjackson/expect/で問題が発生しています。 –

答えて

0

chaiあなたはSinonのようなライブラリを必要とするため、スパイを提供していません。

2の有用な組み合わせを作成しsinon-chai呼ばチャイプラグインがあります:

const chai  = require('chai'); 
const sinon  = require('sinon'); 
const sinonChai = require('sinon-chai'); 
const expect = chai.expect; 

chai.use(sinonChai); 

// Create the spy, using Sinon. 
let spy = sinon.spy(); 

// Call the spy, so we can test it. 
spy(); 

// Assert that the spy has been called. 
expect(spy).to.have.been.called; 
1

あなたが実行しているexpectのバージョン? GitHubのページごとに、expectはあなたがV21を実行している場合は冗談に寄付されています+あなたはhereは、次のコードを使用します@通常のexpect.createSpy()

フォローインストール手順の代わりに冗談モック機能jest.fn()を使用する必要があります、あなたは問題ないはずです。

var spy = jest.fn(); 
spy(); 
expect(spy).toHaveBeenCalled(); 
関連する問題