首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >艾灸的正确用法

艾灸的正确用法
EN

Stack Overflow用户
提问于 2020-08-03 00:09:15
回答 1查看 290关注 0票数 0

我正在尝试使用moxios模拟函数中的axios请求。测试运行得很好,并得到了预期的结果,但我不认为他们实现它的方式根本不是一个最佳实践。有人能给我推荐一种比在测试中使用setTimeout()更好的方法来实现这一点吗?

代码语言:javascript
复制
....

componentDidMount() {
    this.fetchSomething();
}

fetchSomething = () => {
    Axios.get('https://someurl.com/api/1')
        .then((response) => {
            this.setState({ data: response.data.data });
        })
        .catch((error) => {
            this.setState(error);
        })
}
....

我写的测试:

代码语言:javascript
复制
beforeEach(() => {
    moxios.install();
});

afterEach(() => {
    moxios.uninstall();
})

it('should ', async (done) => {
    moxios.wait(() => {
        const request = moxios.requests.mostRecent();
        request.respondWith({
            status: 200,
            response: { data: 'hello' }
        })
    });
    const wrapper = shallow(<TestComponent />);

    setTimeout(() => {
        expect(wrapper.instance().state.data).toEqual('hello')
        done();
    }, 500)
});
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-08-03 02:11:01

我认为这是一个更好的选择。

代码语言:javascript
复制
it('should ', (done) => {
 moxios.wait(() => {
     const request = moxios.requests.mostRecent();
     request.respondWith({
        status: 200,
        response: { data: 'hello'}
     }).then(() => {
        expect(wrapper.instance().state.data).toEqual('hello');
        done()
     })
 });
});
const wrapper = shallow(<TestComponent />);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63218239

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档