node.js 尝试使用 supertest 检查响应正文 - 出现错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14339316/
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
Trying to use supertest to check the body of response - getting an error
提问by Scott Switzer
I am trying to use supertest for some testing. Here is the code snippet that I am trying to test:
我正在尝试使用 supertest 进行一些测试。这是我要测试的代码片段:
it("should create a new org with valid privileges and input with status 201", function(done) {
request(app)
.post("/orgs")
.send({ name: "new_org", owner: "[email protected]", timezone: "America/New_York", currency: "USD"})
.expect(201)
.end(function(err, res) {
res.body.should.include("new_org");
done();
});
});
I am getting an error when trying to test the res body:
尝试测试 res 主体时出现错误:
TypeError: Object #<Object> has no method 'indexOf'
at Object.Assertion.include (../api/node_modules/should/lib/should.js:508:21)
at request.post.send.name (../api/test/orgs/routes.js:24:27)
at Test.assert (../api/node_modules/supertest/lib/test.js:195:3)
at Test.end (../api/node_modules/supertest/lib/test.js:124:10)
at Test.Request.callback (../api/node_modules/supertest/node_modules/superagent/lib/node/index.js:575:3)
at Test.<anonymous> (../api/node_modules/supertest/node_modules/superagent/lib/node/index.js:133:10)
at Test.EventEmitter.emit (events.js:96:17)
at IncomingMessage.Request.end (../api/node_modules/supertest/node_modules/superagent/lib/node/index.js:703:12)
at IncomingMessage.EventEmitter.emit (events.js:126:20)
at IncomingMessage._emitEnd (http.js:366:10)
at HTTPParser.parserOnMessageComplete [as onMessageComplete] (http.js:149:23)
at Socket.socketOnData [as ondata] (http.js:1367:20)
at TCP.onread (net.js:403:27)
Is this a bug in supertest, or am I formatting my test incorrectly? Thanks
这是超级测试中的错误,还是我的测试格式不正确?谢谢
回答by kabuko
Alternatively, this should work too:
或者,这也应该有效:
res.body.should.have.property("name", "new_org");
Also, just a note but logically I think it makes sense to put this in another call to expectsinstead of in the final callback. This function can also be re-used, so I tend to put it somewhere reusable when possible:
另外,只是一个注释,但从逻辑上讲,我认为将它放在另一个调用中expects而不是在最终回调中是有意义的。这个函数也可以重用,所以我倾向于把它放在可以重用的地方:
var isValidOrg = function(res) {
res.body.should.have.property("name", "new_org");
};
it("should create a new org with valid privileges and input with status 201", function(done) {
request(app)
.post("/orgs")
.send({ name: "new_org", owner: "[email protected]", timezone: "America/New_York", currency: "USD"})
.expect(201)
.expect(isValidOrg)
.end(done);
});
Now you could imagine you're testing a GETfor /orgs/:orgIdand you could just re-use the same validation.
现在您可以想象您正在测试一个GETfor/orgs/:orgId并且您可以重新使用相同的验证。
回答by Matthew Barnden
if your res.body is an array you need to provide the index of the object so res.body[res.body.length -1].name.should.equal("new_org")- if your property is the last in the array and not ordered
如果你的 res.body 是一个数组,你需要提供对象的索引res.body[res.body.length -1].name.should.equal("new_org")- 如果你的属性是数组中的最后一个并且没有排序
回答by Sivakumar Natarajan
To test a response body, you can just include the expected response in expect,
要测试响应正文,您只需在 中包含预期响应expect,
const { describe, it } = require('mocha');
const supertest = require('supertest');
describe('Validate API calls', () => {
it('create session post request should fail for invalid credentials', (done) => {
const data = { user_name: 'incorrect_username', password: 'INVALID' };
supertest(app).post('/api/session')
.send(data)
.expect('Content-Type', /json/)
.expect({ name: 'AuthenticationError', message: 'Unauthorized' })
.expect(401, done);
});
});
source: https://willi.am/blog/2014/07/28/test-your-api-with-supertest/
来源:https: //willi.am/blog/2014/07/28/test-your-api-with-supertest/
回答by Scott Switzer
This can be rewritten as follows:
这可以改写如下:
res.body.name.should.equal("new_org");
Which will fix the error.
这将修复错误。

