Im looking over Chai documentation and im wondering because on site i don't see any explanation why is this not remended way of doing this. What can go wrong using this and what's bad about it?
I'm testing my API calls if it returns 100 objects in array. Since i don't have so many entrys in my database, i want to use this code.
Code example on Chai site:
expect('foo').to.have.lengthOf(3); // Remended
expect('foo').to.have.lengthOf.at.most(4); // Not remended
My test example:
it('should return 100 object by default, if limit set to 0', (done) => {
chai.request(server)
.get('/api/v1/assets?limit=0')
.then((res) => {
expect(res).to.have.status(200);
expect(res.body).to.be.a('array');
expect(res.body).to.have.lengthOf.at.most(100);
done();
})
.catch((err) => {
done(err)
})
});
Im looking over Chai documentation and im wondering because on site i don't see any explanation why is this not remended way of doing this. What can go wrong using this and what's bad about it?
I'm testing my API calls if it returns 100 objects in array. Since i don't have so many entrys in my database, i want to use this code.
Code example on Chai site:
expect('foo').to.have.lengthOf(3); // Remended
expect('foo').to.have.lengthOf.at.most(4); // Not remended
My test example:
it('should return 100 object by default, if limit set to 0', (done) => {
chai.request(server)
.get('/api/v1/assets?limit=0')
.then((res) => {
expect(res).to.have.status(200);
expect(res.body).to.be.a('array');
expect(res.body).to.have.lengthOf.at.most(100);
done();
})
.catch((err) => {
done(err)
})
});
Share
edited Apr 9, 2019 at 7:20
Zeeecho
asked Apr 9, 2019 at 7:11
ZeeechoZeeecho
1051 silver badge10 bronze badges
1 Answer
Reset to default 8It's not remended because it's just not very specific for an assertion. If you use at.most.(100)
and your API returns 50 or 1 or even an empty array, it will still pass. The documentation is making the assumption that most people would want their test to fail if it returns an empty array, and would prefer to check for an exact number.
The ideal thing would be to create 101 entries in your database (perhaps through some other endpoint?) and then check for exactly 100 to be returned. But if that's very difficult, you could at least check for more than 0, but less than 100
expect(res.body).to.have.lengthOf.at.least(1).and.lengthOf.at.most(100);
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745056658a4608708.html
评论列表(0条)