【问题标题】:Superagent res.text is undefined超级代理 res.text 未定义
【发布时间】:2014-05-15 14:16:23
【问题描述】:

我正在尝试使用 Mocha 和 SuperAgent 的 TDD 方法,但是当来自 SuperAgent 的 res.text 不知何故未定义时卡住了。

测试:

it('should return 2 given the url /add/1/1', function(done) {
        request
            .get('/add/1/1')
            .end(function(res) {

                res.text.should.equal('the sum is 2');
                done();
            });
    });

代码:

router.get('/add/:first/:second', function(req, res) {
    var sum = req.params.first + req.params.second;
    res.send(200, 'the sum is ' + sum);
});

【问题讨论】:

  • 我都试过了,应该都支持。
  • 您检查res.statusCode 以确保它是200?
  • 不错,它是 404!
  • 这个工作:.get('http://localhost:3000/add/1/1')
  • 你如何设置请求

标签: node.js express mocha.js superagent


【解决方案1】:

正如 cmets 中提到的那样,您可能一开始就没有获得 200。

如果是这种情况,我总是在我的 .end 之前添加一个 .expect(200) 以失败并显示更有意义的消息:

it('should return 2 given the url /add/1/1', function(done) {
        request
            .get('/add/1/1')
            .expect(200)
            .end(function(res) {

                res.text.should.equal('the sum is 2');
                done();
            });
    });

【讨论】:

  • 我对 expect 技巧投了 +1 票,但它似乎根本不起作用,因为该函数未定义,在 superagent 文档中没有提到,源代码中也没有这个词全部。那么expect 的诡计从何而来?
  • 我找到了,expectsuperagent 中不可用,但在supertest 中可用(例如在此stackoverflow.com/a/22119119/2626313 中解释)
猜你喜欢
  • 2016-03-28
  • 1970-01-01
  • 2015-10-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多