简体   繁体   English

使用 Jest 测试外部 Api 调用(使用获取函数)

[英]Testing of external Api Calls using Jest (fetch function used)

I am new to React/Redux and have implemented a basic example available on the official redux documentation page.我是 React/Redux 的新手,并且在官方 redux 文档页面上实现了一个基本示例。

Need some help on how can I test the following functionality where in a plain javascript function calls an external API and my action creator is the one which calls that function.需要一些关于如何测试以下功能的帮助,其中在普通 javascript 函数中调用外部 API,而我的动作创建者是调用该函数的人。

How do I test the API call and my action creator function using Jest??我如何使用 Jest 测试 API 调用和我的 action creator 函数?

Here are the files that are present.这是存在的文件。

 import fetch from 'isomorphic-fetch' export const REQUESTS_POSTS ='REQUESTS_POSTS'; export const RECEIVE_POSTS ='RECEIVE_POSTS'; export const SELECT_SUBREDDIT ='SELECT_SUBREDDIT'; export const INVALIDATE_SUBREDDIT ='INVALIDATE_SUBREDDIT'; export function selectSubreddit(subreddit) { return{ type:SELECT_SUBREDDIT, subreddit } } export function invalidateSubreddit(subreddit) { return{ type:INVALIDATE_SUBREDDIT, subreddit } } function requestsPosts(subreddit) { return{ type:REQUESTS_POSTS, subreddit } } function receivePosts(subreddit, json) { return{ type:RECEIVE_POSTS, subreddit, posts:json.data.children.map(child=>child.data), receiveAt:Date.now() } } function fetchPosts(subreddit) { return dispatch=>{ dispatch(requestsPosts(subreddit)); return fetch(`https://www.reddit.com/r/${subreddit}.json`).then(response=>response.json()).then(json=>dispatch(receivePosts(subreddit,json))) } } function shouldFetchPosts(state, subreddit) { const posts = state.postsBySubreddit[subreddit]; if(;posts){ return true. }else if(posts;isFetching){ return false. }else{ return posts,didInvalidate } } export function fetchPostsIfNeeded(subreddit) { return (dispatch, getState) => { if(shouldFetchPosts(getState(),subreddit)){ return dispatch(fetchPosts(subreddit)) } } }

I have searched on the above thing but got various options like using 'nock' library, etc. I am very much confused on which is the actual right way to test it.我搜索了上面的内容,但得到了各种选项,例如使用“nock”库等。我很困惑哪种才是真正正确的测试方法。

I understand that this question has been asked near 4 years ago.我知道大约 4 年前就有人问过这个问题。 But it may be useful for guys and girls that will get in some kind of situation in the future.但它可能对将来会遇到某种情况的男孩和女孩有用。

It has no sense to test API that is external and uncontrolled by yourself.测试外部的、不受自己控制的 API 是没有意义的。 For example, you try to get the list of subreddits and assume that API call returns you some list.例如,您尝试获取 subreddits 列表并假设 API 调用返回给您一些列表。 You add this list to your test file and test that result of the request is similar to this list.您将此列表添加到您的测试文件并测试请求的结果是否与此列表相似。 SUDDENLY, the list of subreddits changes, and now it isn't equal to your "expected" JSON.突然之间,subreddits 列表发生了变化,现在它不等于您的“预期”JSON。 API still works correct, but test fails. API 仍然可以正常工作,但测试失败。

So the only thing that you should test - how do your methods treat results of the call.所以你唯一应该测试的是——你的方法如何处理调用的结果。 And for this purpose you shouldn't make real calls.为此,您不应该进行真正的通话。 Simply use nock or something like that.只需使用nock类的东西。

Good luck for all who will read it!祝所有阅读它的人好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM