Testing requests that redirect with mocha/supertest in node

后端 未结 4 644
北恋
北恋 2021-02-01 02:35

I can\'t seem to get the following integration test to pass in an express project using mocha, supertest, and should (and coffeescript).


The test<

4条回答
  •  滥情空心
    2021-02-01 02:46

    I was trying to write some integration tests for requests that redirected as well, and found a good example by the author of the module himself here.

    In TJ's example, he's using chaining, so I used something like that as well.

    Consider a scenario in which a logged-in user is redirected to the home page when he or she logs out.

    it('should log the user out', function (done) {
      request(app)
        .get('/logout')
        .end(function (err, res) {
          if (err) return done(err);
          // Logging out should have redirected you...
          request(app)
            .get('/')
            .end(function (err, res) {
              if (err) return done(err);
              res.text.should.not.include('Testing Tester');
              res.text.should.include('login');
              done();
            });
        });
    });
    

    Depending on how many redirects you have, you might have to nest a few callbacks, but TJ's example should suffice.

提交回复
热议问题