【发布时间】:2018-07-31 02:52:43
【问题描述】:
假设我有以下文件。
此文件从库中导入promiseFunction,我想测试doSomething() 函数。
特别是,当 Promise 解决或失败时断言状态
// file: MyComponent.js
import promiseFunction from 'somewhere';
class MyClass extends Component {
doSomething = () => {
promiseFunction()
.then((data) => {
this.setState({...this.state, name: data.name});
}.catch(() => {
this.setState({...this.state, error: 'error'});
});
}
}
我如何模拟正在导入的promiseFunction。或者实际上只是任何正在导入的函数。
// file: MyClass.spec.js
it('sets error in state when doSomething() is rejected', () => {
const wrapper = shallow(<MyClass />);
// How do I mock promiseFunction here?
wrapper.instance().doSomething();
expect(wrapper.state().error).toEqual('error');
});
【问题讨论】:
标签: javascript testing jestjs sinon enzyme