javascript 尝试设置状态代码时 res.status 不是函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47502851/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
res.status not a function when trying to set the status code
提问by David Wood
When attempting to meet the specification set by unit tests supplied, when attempting to return the status code for a method I am hit with
当试图满足由提供的单元测试设置的规范时,当试图返回我遇到的方法的状态代码时
TypeError: res.status is not a function
类型错误:res.status 不是函数
when running the function createUser in the API implementation. It happens with every method call, such as res.send, res.sendStatus etc. Even if I add res.status() to the testing to set it there, it returns the same error.
在 API 实现中运行函数 createUser 时。它发生在每个方法调用中,例如 res.send、res.sendStatus 等。即使我将 res.status() 添加到测试中以将其设置在那里,它也会返回相同的错误。
apiTests.js
apiTests.js
let chai = require('chai');
let expect = chai.expect;
let sinon = require('sinon');
let sinonChai = require('sinon-chai');
chai.use(sinonChai);
let model = require('../tron_model.js'); // for stubbing only
let api = require('../tron_api.js');
describe('API', function() {
describe('creating users', function() {
it('should return 201 on creating user', function () {
let mockModel = sinon.stub(new model.TronModel());
mockModel.addUser.onFirstCall().returns(new model.User('user','pass'));
let req = {body: {username: 'goatzilla', password: 'x'}};
let res = {end: function(){}};
api.init(mockModel);
api.createUser(req, res);
expect(res.statusCode).to.equal(201);
expect(mockModel.addUser).to.have.callCount(1);
});
});
});
tron_api.js
tron_api.js
let model = undefined;
let api = exports;
api.init = function(modelArg) {
model = modelArg;
};
api.createUser = function(req, res) {
model.addUser(req.body.username, req.body.password);
console.log(res);
res.status(201);
};
回答by jfriend00
You mocked a resobject with this code:
您res使用以下代码模拟了一个对象:
let res = {end: function(){}};
that does not have a .status()method and then passed that to your api.createUser()function which expects to call res.status()(a method that is not on your mocked object). A mocked object will need to have every method on it that your code calls.
没有.status()方法,然后将其传递给您api.createUser()希望调用的函数res.status()(一个不在您的模拟对象上的方法)。模拟对象需要具有代码调用的所有方法。
In addition, you are also testing the property res.statusCodewith this:
此外,您还使用以下内容测试该属性res.statusCode:
expect(res.statusCode).to.equal(201);
which also does not exist on your mocked object. While res.status()is a built-in capability in Express, res.statusCodeis not a documented property (though it does appear to exist).
这在您的模拟对象上也不存在。虽然res.status()是 Express 中的内置功能,res.statusCode但不是记录在案的属性(尽管它似乎确实存在)。
You could add to your mocked resobject like this:
您可以res像这样添加到您的模拟对象中:
let res = {
end: function(){}
status: function(s) {this.statusCode = s; return this;}
};
To get it to pass those two tests.
让它通过这两个测试。

