【问题标题】:Testing Node with Superagent and Mocha使用 Superagent 和 Mocha 测试节点
【发布时间】:2012-02-28 03:05:46
【问题描述】:

当我在下面运行此代码时,用户已登录并且浏览器被重定向到新路由 home。如何构建下面的测试以验证 /home 是否被重定向到。

应用程序运行良好,我只是想按顺序整理我的测试用例。

谢谢

   it('should login user',
    function(done) {
        request
        .post(url.parse('http://localhost:3000/login'))
        .send({
            userName: "load@xyz.com", password: "xyzpassword"
        })
        .end(function(res) {
            res.statusCode.should.equal(302);
            done();
        })
    });

附加信息...代码有效,但堆栈转储失败,因此我无法验证测试是否成功

 1) authentication_tests should login user:
     TypeError: first argument must be a string, Array, or Buffer
      at ClientRequest.write (http.js:601:11)
      at ClientRequest.end (http.js:681:16)
      at Request.end (/Users/aaronksaunders/dev/node_stuff/sqchic/node_modules/superagent/lib/node/index.js:602:7)
      at Request.redirect (/Users/aaronksaunders/dev/node_stuff/sqchic/node_modules/superagent/lib/node/index.js:459:8)
      at ClientRequest.<anonymous> (/Users/aaronksaunders/dev/node_stuff/sqchic/node_modules/superagent/lib/node/index.js:569:49)
      at ClientRequest.emit (events.js:64:17)
      at HTTPParser.<anonymous> (http.js:1349:9)
      at HTTPParser.onHeadersComplete (http.js:108:31)
      at Socket.ondata (http.js:1226:22)
      at Socket._onReadable (net.js:683:27)
      at IOWatcher.onReadable (net.js:177:10)

【问题讨论】:

    标签: node.js mocha.js superagent


    【解决方案1】:

    来自superagent docs

    响应头字段

    res.header 包含一个已解析的标头字段对象,字段名称小写,就像节点一样。例如 res.header['content-length']。

    所以你可以检查一下:

    (/home$/).test res.header['location']
    

    对于您的重定向测试用例。

    【讨论】:

    • 问题是代码可以工作,但测试工具因堆栈转储而失败,请参阅上面的编辑
    • 这可能是因为您将从 url.parse 返回的对象传递给 request.post。只需将 URL 作为字符串直接传递给 request.post。
    猜你喜欢
    • 2015-10-10
    • 2014-08-11
    • 2013-01-23
    • 1970-01-01
    • 2017-11-29
    • 2019-05-17
    • 2014-11-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多