【发布时间】:2019-01-13 09:26:27
【问题描述】:
我有一个react 组件,它调用作为prop 传入的async 函数,然后调用then 函数中的另一个函数。
我在下面简单化了它作为插图。
例如
const Form = ({ doSomething, closeModal }) =>
<form onSubmit={(e) => {doSomething().then(() => closeModal())
}}>
...
</form>
我正在尝试测试 closeModal 是这样调用的:
it('should doSomething then call closeModal', () => {
const doSomethingStub = sinon.stub().resolves()
const closeModalStub = sinon.stub()
const props = {
doSomething: doSomethingStub,
closeModal: closeModalStub
}
const wrapper = shallow(<Form {...props}/>)
wrapper.find(`form`).simulate(`submit`)
expect(doSomethingStub.called).toEqual(true)
expect(closeModalStub.called).toEqual(true)
})
在我的示例中,只有第一个期望是正确的。我对 sinon.stub 设置做错了吗?或者我期待什么?感觉像是小事,但我无法确定
【问题讨论】:
标签: javascript reactjs unit-testing sinon stub