Skip to content

How to access res.body and res.text? #68

Closed
andyshinn opened this Issue Jul 23, 2013 · 3 comments

3 participants

@andyshinn

My apologies if I am missing something obvious. But I was trying to do something like #10 and can't figure out how with .body or .text. I'd like to run a test with some additional assertions like this:

  describe('with valid email address', function() {
    it('should return status 201', function(done) {
      request('https://api.myapidomain.com')
        .post('/users')
        .send({
          'email': 'myuser',
          'password': 'mypass'
        })
        .set('Accept', 'application/json')
        .expect(201)
        .expect('Content-Type', /application\/json/)
        .body(function(body) {
           expect(body.id).to.equal(500);
        })
        .end(function(err, res) {
          if (err) return done(err);
          done();
        });
    });

If I put the assertion in .end() then done() gets called before the test is finished. Is there a way to do arbitrary assertions before .end()?

@h2non
h2non commented Sep 1, 2013

Do it at end() callback

...
  .end(function(err, res) {
     if (err) return done(err);
     expect(res.body.id).to.equal(500);
     // or 
     expect(res.text).to.equal('{ "id": 500 }');
     done();
   });
...
@andyshinn

Thanks! I eventually worked around this with some more wrappers on my tests.

@andyshinn andyshinn closed this Sep 5, 2013
@zzz6519003

shinny~

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.