Permalink
Browse files

add response body assertions

  • Loading branch information...
1 parent 5899d39 commit b974ce6d8387f642a1e5a3b81c0e2c7e1baa31a2 @tj tj committed Jun 26, 2012
Showing with 26 additions and 2 deletions.
  1. +8 −2 lib/test.js
  2. +18 −0 test/supertest.js
View
10 lib/test.js
@@ -26,7 +26,7 @@ module.exports = Test;
function Test(app, method, path) {
Request.call(this, method, path);
this.app = app;
- this.fields = {};
+ this._fields = {};
}
/**
@@ -61,7 +61,7 @@ Test.prototype.expect = function(val, fn){
case 'string':
// header field
if ('string' == typeof fn) {
- this.fields[val] = fn;
+ this._fields[val] = fn;
fn = arguments[2];
} else {
this._body = val;
@@ -104,12 +104,18 @@ Test.prototype.end = function(fn){
Test.prototype.assert = function(res, fn){
var status = this._status;
+ var body = this._body;
// status
if (status && res.status !== status) {
return fn(new Error('expected ' + status + ' response, got ' + res.status), res);
}
+ // body
+ if (null != body && body !== res.text) {
+ return fn(new Error('expected "' + body + '" response body, got "' + res.text + '"'));
+ }
+
fn(null, res);
};
View
18 test/supertest.js
@@ -54,4 +54,22 @@ describe('request(app)', function(){
});
})
})
+
+ describe('.expect(body[, fn])', function(){
+ it('should assert the response body', function(done){
+ var app = express();
+
+ app.get('/', function(req, res){
+ res.send('heys');
+ });
+
+ request(app)
+ .get('/')
+ .expect('hey')
+ .end(function(err, res){
+ err.message.should.equal('expected "hey" response body, got "heys"');
+ done();
+ });
+ })
+ })
})

0 comments on commit b974ce6

Please sign in to comment.