【发布时间】:2016-06-27 10:04:20
【问题描述】:
我正在尝试为我的控制器编写一个简单的测试。我使用来自 Sails.js 的 documentation
UserController.test.js:
var request = require('supertest');
describe('UserController', function () {
describe('#login()', function () {
it('should redirect to / indexpage', function (done) {
request(sails.hooks.http.app)
.post('/login')
.send({name: 'Stifflers', password: 'Mom'})
.expect(302)
.expect('Location', '/', done);
});
});
});
来自AuthController.js的相关代码:
...
// is authenticated
res.writeHead(302, {
'Location': "/"
});
res.end();
...
我用 npm test 运行测试并得到这个错误:
错误:预期 302“找到”,得到 200“确定”
当我将测试中的 .expect(302) 更改为 .expect(200) 时,我收到下一个错误:
错误:预期的“位置”标头字段
我已经尝试按照文档中的方式进行操作,为什么它不起作用?
【问题讨论】:
-
你为什么不用
res.redirect(302, '/')? -
@Bonanza 没关系,我们得到相同的效果。 1:expressjs.com/en/api.html#res.redirect 2:nodejs.org/api/…
-
您确认它实际上正在执行正确的路线吗?好像没有。
-
我在 config/routes.js: 'post /login': 'AuthController.login', 上面的重定向在 AuthController.login() 函数中。
-
天啊,我的错……天啊……我的测试密码不正确。这就是一切。但是我怎么能早点发现这个愚蠢的错误???天哪
标签: node.js testing sails.js mocha.js supertest