2017-03-03 6 views
0

私はスタブ付き約束の中の関数が呼び出されたことをテストしようとしています。残念ながら、その機能に関するスパイは、テストの後にのみ呼び出されるようです。ここに私のReactコードの簡略版があります。約束の中のsinon spy.calledOnceはfalseを返します

export default class TheComponent extends React.Component { 
    constructor(props) { 
    super(props); 
    } 

    onSend(data) { 
    AUtility.aPromise(data).then(() => { 
     this.props.onSend(data); 
     console.log("Props onSend was called!") 
    }); 
    } 

    render() { 
    return null; 
    } 
} 

ここで実行しようとするテストです。

it('should call the props function', (done) => { 
    const onSendSpy = spy(); 
    const promiseStub = stub(AUtility, 'aPromise').resolves('mock string'); 
    wrapper = shallow(<TheComponent onSend={onSendSpy} /> 
    wrapper.instance().onSend(); 
    expect(promiseStub.calledOnce).to.be.true; //this passes 
    expect(onSendSpy.calledOnce) //this fails 
    done(); 
}); 

私は最初の主張が通るテストではなく、秒を実行するたびに。しかし、私のコードのprintステートメントは、テスト中に文字列を出力します。 done()関数は、約束が返ってからブロックが完全に実行された後ではなく、テストを終了させるように見えます。どうすればこのテストに合格することができますか?

答えて

0

私は専門家ではありませんが、setTimeout関数で2つのexpect文をラップすることはできますか?

setTimeout(() => { expect(promiseStub.calledOnce).to.be.true; expect(onSendSpy.calledOnce); done(); }, 0);

関連する問題