为什么 supertest 在一切通过时返回错误?
Why is supertest returning an error when everything passes?
我的测试看起来像:
it('should create a user with an email address and password', function(done) {
return request.post('/v1/users').send(defaultUser).expect(200).expect(function(res) {
console.log(res.body);
res.body.should.have.property('id');
res.body.should.have.property('name');
res.body.should.have.property('email');
res.body.should.have.property('username');
return res.body.should.have.property('entities')["with"].lengthOf(1);
}).end(done);
});
res.body
长得像
{ id: 17,
name: 'Satoshi Nakamoto',
email: 'snakamoto@mysite.com',
username: 'snakamoto',
api_key: '510e55d2c15b0617757bd463b3653ec8',
entities: [ { id: 17, name: 'Satoshi Nakamoto Default Entity' } ] }
因此它具有所有适当的字段。我得到的错误是
1) User Interactions should create a user with an email address and password:
Error: [object Object]
at Test.assert (/Users/shamoon/Sites/mysite/mysite-api/node_modules/supertest/lib/test.js:218:48)
at Server.assert (/Users/shamoon/Sites/mysite/mysite-api/node_modules/supertest/lib/test.js:132:12)
at Server.g (events.js:180:16)
at Server.emit (events.js:92:17)
at net.js:1276:10
at process._tickCallback (node.js:419:13)
我做错了什么?
这是问题所在:
return res.body.should.have.property('entities')["with"].lengthOf(1);
Supertest 处理来自 expect
调用的虚假值 return,该调用采用函数表示 "all is okay" 和非虚假值表示 "there's been a problem"。你 return 很可能不是虚假的。 should
的大多数实现都执行 return 允许标记链接的对象(例如 foo.should.something(...).and.something(...).and...
)。对象是非假的,因此 expect
认为测试失败。
我的测试看起来像:
it('should create a user with an email address and password', function(done) {
return request.post('/v1/users').send(defaultUser).expect(200).expect(function(res) {
console.log(res.body);
res.body.should.have.property('id');
res.body.should.have.property('name');
res.body.should.have.property('email');
res.body.should.have.property('username');
return res.body.should.have.property('entities')["with"].lengthOf(1);
}).end(done);
});
res.body
长得像
{ id: 17,
name: 'Satoshi Nakamoto',
email: 'snakamoto@mysite.com',
username: 'snakamoto',
api_key: '510e55d2c15b0617757bd463b3653ec8',
entities: [ { id: 17, name: 'Satoshi Nakamoto Default Entity' } ] }
因此它具有所有适当的字段。我得到的错误是
1) User Interactions should create a user with an email address and password:
Error: [object Object]
at Test.assert (/Users/shamoon/Sites/mysite/mysite-api/node_modules/supertest/lib/test.js:218:48)
at Server.assert (/Users/shamoon/Sites/mysite/mysite-api/node_modules/supertest/lib/test.js:132:12)
at Server.g (events.js:180:16)
at Server.emit (events.js:92:17)
at net.js:1276:10
at process._tickCallback (node.js:419:13)
我做错了什么?
这是问题所在:
return res.body.should.have.property('entities')["with"].lengthOf(1);
Supertest 处理来自 expect
调用的虚假值 return,该调用采用函数表示 "all is okay" 和非虚假值表示 "there's been a problem"。你 return 很可能不是虚假的。 should
的大多数实现都执行 return 允许标记链接的对象(例如 foo.should.something(...).and.something(...).and...
)。对象是非假的,因此 expect
认为测试失败。