Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
437 views
in Technique[技术] by (71.8m points)

node.js - Testing requests that redirect with mocha/supertest in node

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

should  = require('should')
request = require('supertest')
app     = require('../../app')

describe 'authentication', ->
  describe 'POST /sessions', ->
    describe 'success', (done) ->
      it 'displays a flash', (done) ->
        request(app)
          .post('/sessions')
          .type('form')
          .field('user', 'username')
          .field('password', 'password')
          .end (err, res) ->
            res.text.should.include('logged in')
            done()

Relevant application code

app.post '/sessions', (req, res) ->
  req.flash 'info', "You are now logged in as #{req.body.user}"
  res.redirect '/login'

Failure

1) authentication POST /sessions success displays a flash:
   AssertionError: expected 'Moved Temporarily. Redirecting to //127.0.0.1:3456/login' to include 'logged in'

Obviously, the application code doesn't do anything useful. I'm just trying to get the test to pass.

Putting the expectation (res.text.should.include('logged in')) outside the end function and inside the expect function yields the same results. I've also tried a variation of the function calls, for example removing the .type('form') call, and using .send(user: 'username', password: 'password') instead of the two .field() calls.

If it means anything, sending a curl POST request to the the app when it's running locally yields the same output (Moved Temporarily. Redirecting to //127.0.0.1:3456/login)

I have a feeling this is a trivial error. Possibly something I'm forgetting in the application code or the test code.

Any suggestions?

EDIT 1: It's also worth noting that when clicking the submit button in the browser I get the expected results (a flash message).

EDIT 2: Further investigation shows the output of any redirect results in the Moved Temporarily. Redirecting to ... response body. This makes me wonder if there is a problem in the way I'm exporting the app in app.js.

var express = require('express')
var app = express();

module.exports = app;
question from:https://stackoverflow.com/questions/12272228/testing-requests-that-redirect-with-mocha-supertest-in-node

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There is built-in assertion for this in supertest:

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')
          .expect(302)
          .expect('Location', '/home')
          .end(done)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...