Node.js 在节点中使用mocha/supertest重定向的测试请求
在使用、和(和coffeescript)的express项目中,我似乎无法通过以下集成测试Node.js 在节点中使用mocha/supertest重定向的测试请求,node.js,express,mocha.js,supertest,Node.js,Express,Mocha.js,Supertest,在使用、和(和coffeescript)的express项目中,我似乎无法通过以下集成测试 测试 should = require('should') request = require('supertest') app = require('../../app') describe 'authentication', -> describe 'POST /sessions', -> describe 'success', (done) ->
测试
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'
失败
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')
)放在end函数外部和expect
函数内部会产生相同的结果。我还尝试了函数调用的一种变体,例如删除.type('form')
调用,并使用.send(user:'username',password:'password')
而不是两个.field()
调用
如果有什么意义的话,当应用程序在本地运行时,向应用程序发送curl POST请求会产生相同的输出(临时移动。重定向到//127.0.0.1:3456/login
)
我觉得这是一个小错误。可能是我在应用程序代码或测试代码中忘记了什么
有什么建议吗
编辑1:还值得注意的是,当单击浏览器中的提交按钮时,我会得到预期的结果(一条闪光消息)
编辑2:进一步的调查显示,在临时移动的中,任何重定向结果的输出都会显示出来。正在重定向到…
响应正文。这让我怀疑在app.js中导出应用程序的方式是否存在问题
var express = require('express')
var app = express();
module.exports = app;
对于任何浏览过本页的人来说,这个问题的答案都非常简单。
临时移动。
响应主体是从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')
.end (err, res) ->
res.header['location'].should.include('/home')
done()
只需检查响应头
位置
是否符合您的预期。测试flash消息和视图特定的集成测试应该使用另一种方法来完成。我试图为重定向的请求编写一些集成测试,并发现模块作者自己提供了一个很好的示例
在TJ的例子中,他使用了链锁,所以我也使用了类似的东西
考虑一个场景,其中登录的用户在注销时被重定向到主页
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();
});
});
});
根据重定向的数量,您可能需要嵌套一些回调,但TJ的示例应该足够了。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)
redirects()
将跟随重定向,以便您可以执行常规的视图测试。…还有什么其他方法?我来这里是因为我正在尝试“测试flash消息和视图特定的集成测试应使用另一种方法完成”。除了检查标题中的位置,你应该检查res.statusCode是否为302(重定向)。请看一看:这可能是一篇旧文章,但是.redirects(1)在我测试时很好地解决了重定向错误。非常感谢。