中使用摩卡/超級終端重定向的測試請求我似乎無法使用mocha,supertest和should(和coffeescript)獲得以下集成測試以通過快速項目。在節點
測試
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()
相關的應用程序代碼
app.post '/sessions', (req, res) ->
req.flash 'info', "You are now logged in as #{req.body.user}"
res.redirect '/login'
失敗
1) authentication POST /sessions success displays a flash:
AssertionError: expected 'Moved Temporarily. Redirecting to //127.0.0.1:3456/login' to include 'logged in'
顯然,應用程序代碼沒有任何用處。我只是想讓測試通過。
將期望值(res.text.should.include('logged in')
)放在結束函數之外,並在expect
函數內部產生相同的結果。我也嘗試了函數調用的一個變體,例如刪除.type('form')
調用,並使用.send(user: 'username', password: 'password')
而不是兩個.field()
調用。
如果這意味着什麼,送捲曲POST請求時,它在本地運行的應用程序產生相同的輸出(Moved Temporarily. Redirecting to //127.0.0.1:3456/login
)
我有一種感覺,這是一個很小的錯誤。可能是我在應用程序代碼或測試代碼中忘記的東西。
有什麼建議嗎?
編輯1:也值得注意的是,當點擊瀏覽器中的提交按鈕時,我會得到預期的結果(一條flash消息)。
編輯2:進一步的調查顯示任何重定向在Moved Temporarily. Redirecting to ...
響應身體的結果的輸出。這讓我想知道在app.js中導出應用程序的方式是否存在問題。
var express = require('express')
var app = express();
module.exports = app;
請看看:http://stackoverflow.com/questions/14001183/how-to-authenticate-supertest-requests-with-passport/37609721#37609721 –