如何从第三方库测试方法?

时间:2018-02-22 02:23:49

标签: reactjs mocha sinon chai enzyme

我正在使用第三方库记录某些事件,在我的组件中进行设置:

在组件componentDidMount中:

this.evenStream = new thirdPartyLib();
this.evenStream.setArgs(args);
this.evenStream.addQueue('the'); // should be 'the-spell' for misspell page
const event1 = new Event1();
const event2 = new Event2();
this.evenStream.addQueue(event1); // adds events in sequence in queue
this.evenStream.addQueue(event2); // adds events in sequence  in queue
this.evenStream.reportEvents(); // reports events previously added to queue, once reported the queue is cleared 

在测试中:

this.wrapper = mount(<component  />); // looks good
this.evenStreamInstance = this.wrapper.instance().evenStream; //returns properties of evenStream
this.eventStub = sinon.stub(this.evenStreamInstance, 'reportEvents');
expect(this.wrapper).to.be.ok; // looks good
expect(this.evenStreamInstance).to.exist; // looks good
expect(this.eventStub).to.have.been.called; // returns error

AssertionError: expected reportEvents to have been called at least once, but it was never called

我确信事件记录正确,我已经检查了流,所有看起来都很好,我不知道如何测试它。理想情况下,我想测试添加到addQueue的所有事件然后报告,但我无法捕获该信息。

感谢任何帮助。谢谢。

更新 我按照this post使用存根再次尝试,但仍然得到相同的错误

const eventStub = sinon.stub(new thirdPartyLib());
this.wrapper = mount(<component evenStream={eventStub} />); //looks good
this.evenStreamInstance = this.wrapper.instance().evenStream; //returns properties of evenStream
this.evenStreamInstance.reportEvents = sinon.spy(this.evenStreamInstance.reportEvents);

expect(this.wrapper).to.be.ok; // looks good
expect(this.evenStreamInstance).to.exist; //looks good
this.wrapper.update();
expect(this.evenStreamInstance.reportEvents).to.have.been.calledOnce; // gives error

AssertionError: expected reportEvents to have been called exactly once, but it was called 0 times

0 个答案:

没有答案