mercredi 21 mars 2018

Testing GET request with Chai returns 404

I am trying to test if the a GET request is successful. When I run the program everything works fine, but when testing with Chai, the GET request fails with status 404.

test block:

describe('/allPosts GET', () => {
  it('it should GET all the pics', (done) => {
    chai.request(server)
        .get('/allPosts')
        .end((err, res) => {
            res.should.have.status(200);
            done();
     });
  });
});

api route

router.get('/allPosts', (req, res) => {
   postsModel.find().sort({_id:-1}).then(allPosts => {
     res.send(allPosts)
   }).catch(err => {
     res.send([err])
   });
});

test report

  1) /allPosts GET
       it should GET all the pics:

      Uncaught AssertionError: expected { Object (domain, _events, ...) } to have status code 200 but got 404
      + expected - actual

      -404
      +200

Any feedback is appreciated.

Aucun commentaire:

Enregistrer un commentaire