我想测试我的容器组件方法。我的容器有一个异步方法,用于加载所有提议并设置为状态。例如:异步测试 - 摩卡和柴 - 确保done()回调被称为
loadProposal(proposalId) {
return axios
.get("http://localhost:9292/api/proposal_drafts/1.json")
.then(response => {
this.setState({
proposal: Immutable.fromJS(response.data)
})
})
}
因此,要测试此方法,我得到组件实例并调用方法(api url被模拟)。
it("sets proposal in the state", (done) => {
const wrapper = shallow(<Container/>)
loadProposalRequest(1)
wrapper.instance().loadProposal(1).then(() => {
chai.expect(wrapper.state().proposal).to.be(Map)
done()
})
})
,但我得到这个错误,从控制台:
Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test.
行动:如果我把一个的console.log(wrapper.state())内则()。日志正确显示我的状态。