gpt4 book ai didi

unit-testing - Nock拦截请求但返回空对象

转载 作者:行者123 更新时间:2023-12-04 21:34:32 24 4
gpt4 key购买 nike

我正在使用 mocha作为测试框架,我正在尝试模拟一个 DELETE使用 fetch 的请求针对返回 HTTP 状态代码的端点 204 .

下面是测试代码:

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);
});
});

这将返回以下输出:
matching <domain> to DELETE <domain>/<path>: true 
(the above line being generated by the .log method in nock)
IS DONE?---> true
RESPONSE---> {}

如您所见,请求被正确拦截,如 log() 所述。和 isDone() nock 方法,但是 response返回的对象是一个空对象,因此无法对返回的 HTTP 状态代码进行断言(在本例中为 204 )

知道我在这里可能遗漏了什么吗?为什么 reply()方法返回一个空对象?

更新

这是 logout的代码方法, remove方法是 fetch 的包装器使用 DELETE 请求HTTP 方法。
logout(token) {
return remove(
this.host,
END_POINTS.DELETE_TOKEN,
{
pathParams: { token },
},
{
Accept: 'application/json',
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`,
},
);
}

最佳答案

我认为 204 不应该有响应 body ,因此您可能需要将其更改为 200。服务器当然可以返回响应,但我认为 fetch 不会处理它。其他包如 request将处理状态为 204 的正文,但此请求包仅适用于服务器端。

也不确定你的包装器做了什么,但我认为你需要使用 response.json() promise 来获得响应。而且 mocha 也可以自动处理 promise,你可以直接返回它们。请参阅下面的完整示例:

const nock = require('nock')
const fetch = require('isomorphic-fetch');
const request = require('request')

const domain = "http://domain.com";
const path = '/some-path';
const token = 'some-token';

const api = {
logout: (token) => {
return fetch(domain + path, {
method: 'DELETE',
headers: {
'Content-Type': 'application/json'
}
});
}
}

describe('something', () => {
it('should logout user with 204 response using request package', (done) => {
nock(domain)
.log(console.log)
.delete(path)
.reply(204, {
status: 204,
message: 'This is a mocked response',
});

request.delete(domain + path, function(err, res) {
console.log(res.body);
done(err);
})
});

it('should logout user', () => {
nock(domain)
.log(console.log)
.delete(path)
.reply(200, {
status: 200,
message: 'This is a mocked response',
});

return api.logout(token)
.then((response) => {
console.log('IS DONE?--->', nock.isDone());
return response.json();
})
.then(function(body) {
console.log('BODY', body);
})
.catch((error) => {
console.log('ERROR--->', error);
});
});
});

这将输出:
  something
matching http://domain.com:80 to DELETE http://domain.com:80/some-path: true
{"status":204,"message":"This is a mocked response"}
✓ should logout user with 204 response
matching http://domain.com:80 to DELETE http://domain.com:80/some-path: true
IS DONE?---> true
BODY { status: 200, message: 'This is a mocked response' }
✓ should logout user

使用了以下 deps:
  "dependencies": {
"isomorphic-fetch": "^2.2.1",
"mocha": "^3.2.0",
"nock": "^9.0.6",
"request": "^2.79.0"
}

我希望它有帮助。

关于unit-testing - Nock拦截请求但返回空对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42000648/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com