{"id":546,"date":"2020-03-17T15:50:11","date_gmt":"2020-03-17T07:50:11","guid":{"rendered":"https:\/\/wp.winter-weng.cc\/work\/?p=546"},"modified":"2020-03-17T15:50:11","modified_gmt":"2020-03-17T07:50:11","slug":"axios-mock-api-call","status":"publish","type":"post","link":"https:\/\/wp.winter-weng.cc\/work\/?p=546","title":{"rendered":"[axios] mock API call"},"content":{"rendered":"\n<p><strong>fake\/mock.js<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\"><code class=\"language-javascript line-numbers\">import axios from \"@\/axios.js\"\nconst MockAdapter = require('axios-mock-adapter')\nconst mock = new MockAdapter(axios) \/\/ This sets the mock adapter on the default instance\nexport default mock<\/code><\/pre>\n\n\n<p><strong>fake\/index.js<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\"><code class=\"language-javascript line-numbers\">import mock from '.\/mock'\nmock.onGet('\/api\/files').reply(() =&gt; {\n    const response = {\n        Status: true,\n        Message: '',\n        Data: null\n    }\n    return [200, response]\n})\n<\/code><\/pre>\n\n\n<p>\u63a5\u8457\u5728 <code>main.js<\/code> \u4e2d\u5f15\u5165\u3002\u4e4b\u5f8c\u82e5\u8981\u79fb\u9664\u4eff\u9020\u8cc7\u6599\uff0c\u53ea\u8981\u8a3b\u89e3\u6b64\u884c\u5373\u53ef\u3002<\/p>\n\n\n<p><strong>main.js<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\"><code class=\"language-javascript line-numbers\">import '.\/fake\/index.js'<\/code><\/pre>\n\n\n<hr class=\"wp-block-separator\" \/>\n\n\n<p>\u88dc\u5145\uff1aonGet \u8207\u547c\u53eb API \u7684\u8def\u5f91\u8981\u5b8c\u5168\u4e00\u81f4\uff0c\u5982\u4e0b\uff1a<\/p>\n\n\n<pre class=\"wp-block-code\"><code class=\"language-javascript line-numbers\">axios.get('\/progress?id=1&amp;token=abcde')\nmock.onGet('\/progress?id=1&amp;token=abcde')\n\/\/ or use Regex\nmock.onGet(\/\\\/progress\\\/?.*\/)<\/code><\/pre>\n\n\n<p>\u5efa\u8b70\u5c07\u53c3\u6578\u984d\u5916\u50b3\u9001\uff1a<\/p>\n\n\n<pre class=\"wp-block-code\"><code class=\"language-javascript line-numbers\">axios.get('\/progress', {\n    params: {\n        id: 1,\n        token: abcde\n    }\n})\n\/\/ mock \u53ef\u4ee5\u4e0d\u7528\u6b63\u898f\u8868\u793a\u5f0f\nmock.onGet('\/progress')\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>fake\/mock.js fake\/index.j&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"pgc_sgb_lightbox_settings":"","footnotes":""},"categories":[4],"tags":[14],"class_list":["post-546","post","type-post","status-publish","format-standard","hentry","category-javascript","tag-axios"],"_links":{"self":[{"href":"https:\/\/wp.winter-weng.cc\/work\/index.php?rest_route=\/wp\/v2\/posts\/546","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wp.winter-weng.cc\/work\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wp.winter-weng.cc\/work\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wp.winter-weng.cc\/work\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wp.winter-weng.cc\/work\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=546"}],"version-history":[{"count":0,"href":"https:\/\/wp.winter-weng.cc\/work\/index.php?rest_route=\/wp\/v2\/posts\/546\/revisions"}],"wp:attachment":[{"href":"https:\/\/wp.winter-weng.cc\/work\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=546"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wp.winter-weng.cc\/work\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=546"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wp.winter-weng.cc\/work\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=546"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}