Skip to content

.expect(201) passes when res.status == 400 #11

Closed
leegee opened this Issue Jul 19, 2012 · 10 comments

7 participants

@leegee
leegee commented Jul 19, 2012

Sorry to create another ticket out of what may be ignorance but...

describe('POST /', function(){
  it('should pass with good img_uri', function(done){
    request(app)
        .post('/')
        .send({
            img_uri: '/foo.png'
        })
        .expect(201)
        .expect('Content-Type', /json/)
        .expect( { ok: 1 } )
        .end(function(err, res){
            console.log( 'BODY: ' );
            console.log( res.body );
            console.log( res.status );
            console.log( 'res.status == 201: '+ res.status == 201 );
            done()
        })
    })
});

The output (in TAP format):

BODY: 
{ ok: 0, msg: 'Missing parameters: img_uri' }
400
false
ok 1 POST / should pass with good img_uri

This seems to show that either the chained .expect() calls are behaving differently than in the examples, or that I failed to understand how they should work.

TIA
Lee

@tj
Sloth member
tj commented Jul 19, 2012

hmm yeah what you expect is correct, I'll take a look

@tj
Sloth member
tj commented Jul 19, 2012

hmm i cant seem to reproduce it, my tests are fine

@leegee
leegee commented Jul 19, 2012

Thanks for checking. I'll try again tomorrow with a minimum test, and post the results.

@silas
silas commented Sep 14, 2012

I get this same behavior if I don't check for err in end and re-throw.

Example: https://gist.github.com/3722812#file_test.js

Here is a gist with a passing and failing example:

git clone git://gist.github.com/3722812.git gist-3722812

I assume this is the expected behavior, but I might be wrong.

@mfrobben

bump. I'm seeing this with a test that I expect to return status 200 yet recieve 500. My call chain is as follows:

    superTest(appTest.app)
        .post(route)
        .set('Content-Type','application/json')
        .send(params)
        .expect(200)
        .end(function(err, res) {
          console.log(err) // error is returned here
          done()
        })

The mocha output (mixed with Console Log) was:
◦ POST /:uid/route with valid params should return 200:
[Error: expected 200 "OK", got 500 "Internal Server Error"]
✓ POST /:uid/route with valid params should return 200

@mfrobben

TJ & Silas - FYI, this syntax fixes the issue. Hope this helps tracking down what's going on.

superTest(appTest.app)
        .post(route)
        .set('Content-Type','application/json')
        .send(params)
        .expect(200,done)

Output:
✖ 1 of 6 tests failed:

1) POST /:uid/route with valid params should return 200:
Error: expected 200 "OK", got 500 "Internal Server Error"

@DjebbZ
DjebbZ commented Oct 2, 2012

I happen to have the same problem, and found how to solve it simply.

Do as @mfrobben says, expect(SOME_CODE, done) when you don't need complete access to the error and the response.

When you need complete access to do custom stuff in the end callback, test the error as follows :

.end(function(err, res) {
  if (err) {
    done(err);
  } else {
    // do your stuff here
  }
});

I first tried without an else statement, but I got this error : Error: done() called multiple times.

@tj
Sloth member
tj commented Oct 5, 2012

if someone can reproduce it with a pull-request test I'll take a better look, low on time ATM

@mfrobben
mfrobben commented Oct 6, 2012

After reading DjebbZ, this can probably be solved with documentation. Submitting a readme pull request

@eeble
eeble commented Jun 30, 2013

I was getting this error but it turned out it was because I wasn't separating my redirect/render code paths properly. I forgot to put a return after the redirect so it ended up redirecting then rendering on the same response object.

@gjohnson gjohnson closed this Oct 19, 2013
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.