Superagent res.text是未定义的
我正在尝试使用Mocha和SuperAgent的TDD方法,但是当SuperAgent的res.text以某种方式未定义的时候卡住了。
testing:
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); });
正如在评论中提到的人,你可能不会得到一个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(); }); });