【问题标题】:Nock intercepts request but returns empty objectNock 拦截请求但返回空对象
【发布时间】:2017-06-19 10:21:53
【问题描述】:

我正在使用mocha 作为测试框架,我正在尝试模拟一个使用fetchDELETE 请求,以针对返回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 方法是使用DELETE HTTP 方法的fetch 请求的包装器。

logout(token) {
  return remove(
    this.host,
    END_POINTS.DELETE_TOKEN,
    {
      pathParams: { token },
    },
    {
      Accept: 'application/json',
      'Content-Type': 'application/json',
      Authorization: `Bearer ${token}`,
    },
  );
}

【问题讨论】:

  • api.logout的代码是什么?这似乎不是nock 提供的东西。在我看来那里可能有错误。
  • 它代表端点的DELETEHTTP 方法,它工作正常并且它似乎被正确拦截,如输出所示,我已经更新了问题以包含代码,我做了之前不要包含它以避免混淆。

标签: unit-testing mocha.js fetch interceptor nock


【解决方案1】:

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

也不确定你的包装器做了什么,但我认为你需要使用 response.json() 承诺来获得响应。而且 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

以下使用的部门:

  "dependencies": {
    "isomorphic-fetch": "^2.2.1",
    "mocha": "^3.2.0",
    "nock": "^9.0.6",
    "request": "^2.79.0"
  }

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 2019-07-14
    • 2016-10-05
    • 2019-09-19
    • 1970-01-01
    • 2019-07-16
    • 2019-09-26
    • 1970-01-01
    • 1970-01-01
    • 2017-02-28
    相关资源
    最近更新 更多