meadowstream
meadowstream

Reputation: 4141

Superagent res.text is undefined

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

Upvotes: 1

Views: 1731

Answers (1)

Alex
Alex

Reputation: 38529

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

Upvotes: 2

Related Questions