2017-03-03 17 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() 함수는 일단 약속이 반환되고 일단 then 블록이 완전히 실행되지 않으면 테스트를 끝내는 것으로 보인다. 이 시험을 통과하려면 어떻게해야합니까?

답변

0

저는 전문가는 아니지만 setTimeout 함수에서 두 개의 expect 문을 래핑 할 수 있습니까?

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