javascript - Why is this not recommended way of checking lengthOf() array in chai? - Stack Overflow

Im looking over Chai documentation and im wondering because on site i don't see any explanation wh

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
Add a ment  | 

1 Answer 1

Reset to default 8

It'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条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信