5

我正在使用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---> {}

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

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

更新

这是该方法的代码logout,该方法是使用HTTP 方法的请求remove的包装器。fetchDELETE

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

1 回答 1

1

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

也不确定你的包装器做了什么,但我认为你需要使用 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"
  }

我希望它有所帮助。

于 2017-02-15T08:24:42.490 回答