我在当前未测试的函数中有一个分支。它是来自 request
操作的错误处理程序(使用同名的 Node 模块)。这是特定的行:
request(url, function (error, response, body) {
if (error) return cb(error);
这是测试:
describe("handles errors", function() {
it("from the request", function (done) {
var api = nock('http://football-api.com')
.get('/api/?Action=today&APIKey=' + secrets.APIKey + '&comp_id=1204')
.reply(500);
fixture.getFixture(FixtureMock, function (err, fixture) {
expect(err).to.exist
done();
});
});
规范失败:
Uncaught AssertionError: expected null to exist
因此,发送 500
状态代码作为没有正文的响应不会在请求回调中导致 error
,或者我正在测试错误的东西。
最佳答案
使用文档中的replyWithError
:
nock('http://www.google.com')
.get('/cat-poems')
.replyWithError('something awful happened');
关于node.js - 使用 Nock 和 Request 模拟 HTTP 请求错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26248419/