My test looks like:
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
looks like
{ id: 17,
name: 'Satoshi Nakamoto',
email: 'snakamoto@mysite.com',
username: 'snakamoto',
api_key: '510e55d2c15b0617757bd463b3653ec8',
entities: [ { id: 17, name: 'Satoshi Nakamoto Default Entity' } ] }
So it has all of the appropriate fields. The error that I get is
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)
What am I doing wrong?
This is the problem:
return res.body.should.have.property('entities')["with"].lengthOf(1);
Supertest treats a falsy value returned from an expect
call that takes a function to mean "all is okay" and a non-falsy value to mean "there's been a problem". What you return is most likely not falsy. Most implementations of should
do return objects which allow chaining of flags (e.g. foo.should.something(...).and.something(...).and...
). Objects are non-falsy and thus make expect
think the test has failed.