我正在尝试使用Mocha和SuperAgent的测试驱动开发方法,但当来自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);
});发布于 2014-05-15 23:39:53
正如有人在评论中提到的那样,你很可能一开始就得不到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();
});
});https://stackoverflow.com/questions/23680873
复制相似问题