node-mocks-http响应无法正确处理数字响应

时间:2018-07-16 09:14:21

标签: node.js unit-testing controller response node-mocks-http

我正在使用nodejs中的node-mocks-http库测试一种控制器方法。以下是测试方法。

let response = buildResponse();
  let request = http_mocks.createRequest({
    method: 'POST',
    url: '/v1/nx-organizations/user',
    body: {
      user: {
        key: key,
        email: email,
        firstName: firstName,
        lastName: lastName
      }
    }
  });

  response.on('end', function () {
    let result = JSON.parse(response._getData());
    should.exist(result);
    assert.equal(response.statusCode, 200);
    assert.equal(result, 1);
    done();
  });

  organizationController.associateUser(request, response)
如果成功,

organizationController.associateUser返回1。但是测试失败,并显示错误消息invalid character while parsing json

当我调试代码时显示:

response.statusCode=1
response.statusMessage="OK"

应为

response.statusCode=200
result=1

有人可以帮我吗?

0 个答案:

没有答案