【问题标题】:IntegrationTest with SuperTest expects 302 gets 200 in Sails.js application使用 SuperTest 的 IntegrationTest 预计 Sails.js 应用程序中的 302 得到 200
【发布时间】: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


【解决方案1】:

叫我笨...错误是由我发送的错误帖子变量引起的。

            .send({email: 'Test@test.ch', password: 'test'})

我必须发送电子邮件而不是姓名!每次我用邮递员用“电子邮件”测试它,在我的代码中我有“名字”。

我看到在 SO 和其他地方有很多类似的问题;也许我的错可以帮助他们更好地检查自己的代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-27
    相关资源
    最近更新 更多