如何测试返回诺言的异步操作? [英] How to test async actions that returns promise?

查看:69
本文介绍了如何测试返回诺言的异步操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

fetchMock.get(`http://localhost:8080/data/v1/shopping-cart.json#/`).then(res => {
            console.log('response  ', res);
 });

我正在将http://localhost:8080/data/v1/shopping-cart.json#/作为基本URL传递给fetchMock.get()方法,并获得以下输出:

I am passing http://localhost:8080/data/v1/shopping-cart.json#/ as a base URL to the fetchMock.get() method and getting the following output:

fetch-mock:无效的参数传递给fetch-mock

fetch-mock: Invalid parameters passed to fetch-mock

可能是什么原因?

推荐答案

首先,您实际上滥用了fetchMock().有关详细信息,请查看其文档.预计您将url和callback作为.get()的一部分传递:

First, you actually misuse fetchMock(). Check their docs on details. It's expected you to pass url and callback as part of .get():

fetchMock.get('http://localhost:8080/data/v1/shopping-cart.json', (url, opts) => {
  return {body: 'aaa', status: 200}
});
fetch('http://localhost:8080/data/v1/shopping-cart.json').then(res => console.log(res));

第二,您不应使用哈希部分(在您的情况下为#/)来模拟URL.浏览器不会将此部分发送到服务器,因此我认为您的模拟可能根本无法工作(直到fetch-mock将此部分剥离为内部模拟,但我不会依赖它).

Second you should not use hash part(#/ in your case) for URL mocked. Browser does not send this part to server so I believe your mock may not work at all(until fetch-mock strips this part for mocks under the hood, but I'd not rely on that).

这篇关于如何测试返回诺言的异步操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆