Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simple documentation update. Closes issue #11 #27

Merged
merged 3 commits into from
Oct 7, 2012
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,26 @@ describe('GET /users', function(){
.expect(200, done);
})
})
```

If you are using the `.end()` syntax with mocha, `.expect()` assertions that fail will
not throw - they will return the assertion as an error to the `.end()` callback. In
order to fail the test case, you will need to rethrow or pass `err` to `done()`, as follows:

```js
describe('GET /users', function(){
it('respond with json', function(done){
request(app)
.get('/user')
.set('Accept', 'application/json')
.expect(200)
.end(function(err, res){
if (err)
done(err) // if response is 500 or 404, test case will fail
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will call done() twice, it needs to be if (err) return done(err), though in this case it could just be.end(done)`

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry bout that, sped through writing the update and pull request while on a flight. Updated the readme in the repo. The reason I don't want to use the end(done()) syntax is because in my test cases, I will use expect(200, done) if I'm just validating the response code and end(function(err, res){}) when I want to check something in the response body or header AND validate the response code.

Thx,
~M

On Oct 6, 2012, at 12:39 PM, TJ Holowaychuk notifications@github.com wrote:

In Readme.md:

@@ -50,6 +50,26 @@ describe('GET /users', function(){
})


+  If you are using the `.end()` syntax with mocha, `.expect()` assertions that fail will
+  not throw - they will return the assertion as an error to the `.end()` callback. In
+  order to fail the test case, you will need to rethrow or pass `err` to `done()`, as follows:
+
+```js
+describe('GET /users', function(){
+  it('respond with json', function(done){
+    request(app)
+      .get('/user')
+      .set('Accept', 'application/json')
+      .expect(200)
+      .end(function(err, res){
+        if (err)
+          done(err) // if response is 500 or 404, test case will fail
this will call done() twice, it needs to be if (err) return done(err), though in this case it could just be.end(done)`


Reply to this email directly or view it on GitHub.

done()
});
})
})
```

Anything you can do with superagent, you can do with supertest - for example multipart file uploads!
Expand Down