rfc*_*484 5 unit-testing mocha.js fetch interceptor nock
我正在使用mocha作为测试框架,我正在尝试模拟一个DELETE请求,该请求对返回HTTP状态代码的端点使用fetch204.
这是测试代码:
it('should logout user', (done) => {
nock(<domain>)
.log(console.log)
.delete(path)
.reply(204, {
status: 204,
message: 'This is a mocked response',
});
api.logout(token)
.then((response) => {
console.log('IS DONE?--->', nock.isDone());
console.log('RESPONSE--->', response);
done();
})
.catch((error) => {
console.log('ERROR--->', error);
});
});
Run Code Online (Sandbox Code Playgroud)
这将返回以下输出:
matching <domain> to DELETE <domain>/<path>: true
(the above line being generated by the .log method in nock)
IS DONE?---> true
RESPONSE---> {}
Run Code Online (Sandbox Code Playgroud)
正如你所看到的请求被正确截获由陈述log()和isDone()箭扣方法,但是response返回的对象是一个空的对象,因此它不可能做出关于返回的HTTP状态代码断言(在这个例子中204)
知道我在这里可能缺少什么?,为什么该reply()方法返回一个空对象?
UPDATE
以下是该logout方法的代码,该remove方法是fetch使用DELETEHTTP方法的请求的包装器.
logout(token) {
return remove(
this.host,
END_POINTS.DELETE_TOKEN,
{
pathParams: { token },
},
{
Accept: 'application/json',
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`,
},
);
}
Run Code Online (Sandbox Code Playgroud)
我认为 204 不应该有响应正文,因此您可能需要将其更改为 200。服务器当然可以返回响应,但我认为 fetch 不会处理它。其他包(如request)将处理 204 状态的正文,但此请求包仅适用于服务器端。
\n\n也不确定你的包装器做了什么,但我认为你需要使用response.json()承诺来获取响应。而且 mocha 还可以自动处理 Promise,你只需返回它们即可。请参阅下面的完整示例:
\n\nconst nock = require(\'nock\')\nconst fetch = require(\'isomorphic-fetch\');\nconst request = require(\'request\')\n\nconst domain = "http://domain.com";\nconst path = \'/some-path\';\nconst token = \'some-token\';\n\nconst api = {\n logout: (token) => {\n return fetch(domain + path, {\n method: \'DELETE\',\n headers: {\n \'Content-Type\': \'application/json\'\n }\n });\n }\n}\n\ndescribe(\'something\', () => {\n it(\'should logout user with 204 response using request package\', (done) => {\n nock(domain)\n .log(console.log)\n .delete(path)\n .reply(204, {\n status: 204,\n message: \'This is a mocked response\',\n });\n\n request.delete(domain + path, function(err, res) {\n console.log(res.body);\n done(err);\n })\n });\n\n it(\'should logout user\', () => {\n nock(domain)\n .log(console.log)\n .delete(path)\n .reply(200, {\n status: 200,\n message: \'This is a mocked response\',\n });\n\n return api.logout(token)\n .then((response) => {\n console.log(\'IS DONE?--->\', nock.isDone());\n return response.json();\n })\n .then(function(body) {\n console.log(\'BODY\', body);\n })\n .catch((error) => {\n console.log(\'ERROR--->\', error);\n });\n });\n});\nRun Code Online (Sandbox Code Playgroud)\n\n这将输出:
\n\n something\nmatching http://domain.com:80 to DELETE http://domain.com:80/some-path: true\n{"status":204,"message":"This is a mocked response"}\n \xe2\x9c\x93 should logout user with 204 response\nmatching http://domain.com:80 to DELETE http://domain.com:80/some-path: true\nIS DONE?---> true\nBODY { status: 200, message: \'This is a mocked response\' }\n \xe2\x9c\x93 should logout user\nRun Code Online (Sandbox Code Playgroud)\n\n使用以下部门:
\n\n "dependencies": {\n "isomorphic-fetch": "^2.2.1",\n "mocha": "^3.2.0",\n "nock": "^9.0.6",\n "request": "^2.79.0"\n }\nRun Code Online (Sandbox Code Playgroud)\n\n我希望它有帮助。
\n| 归档时间: |
|
| 查看次数: |
827 次 |
| 最近记录: |