1

I'm trying a TDD approach using Mocha and SuperAgent, but got stuck when the res.text from SuperAgent is somehow undefined.

Test:

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();
            });
    });

Code:

router.get('/add/:first/:second', function(req, res) {
    var sum = req.params.first + req.params.second;
    res.send(200, 'the sum is ' + sum);
});
JimmyRare
  • 3,958
  • 2
  • 20
  • 23

1 Answers1

2

As someone mentioned in the comments, you are likely not getting a 200 in the first place.

I always include a .expect(200) before my .end to fail with a more meaningful message, should this be the case:

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();
            });
    });
Alex
  • 37,502
  • 51
  • 204
  • 332
  • I voted +1 for the ```expect``` trick but it does not seem to work at all as the function is undefined, not mentioned in the superagent docs and there is not such word in the source code at all. So where does the ```expect``` trick come from? – xmojmr Jun 04 '14 at 16:56
  • I found it, the ```expect``` is not available in ```superagent``` but in ```supertest``` (explained e.g. here http://stackoverflow.com/a/22119119/2626313) – xmojmr Jun 10 '14 at 05:55