我正在使用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
有人可以帮我吗?