Testing requests that redirect with mocha/supertest in node

后端 未结 4 638
北恋
北恋 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 03:03

    For anyone who comes across this page, the answer to this question is pretty simple. The Moved Temporarily. response body is what is returned from supertest. See the issue for more details.

    To summarize, I ended up doing something like this.

    should  = require('should')
    request = require('supertest')
    app     = require('../../app')
    
    describe 'authentication', ->
      describe 'POST /sessions', ->
        describe 'success', ->
          it 'redirects to the right path', (done) ->
            request(app)
              .post('/sessions')
              .send(user: 'username', password: 'password')
              .end (err, res) ->
                res.header['location'].should.include('/home')
                done()
    

    Just check that the response header location is what you expect it to be. Testing for flash messages and view specific integration tests should be done using another method.

提交回复
热议问题