Permalink
Browse files

Cleanup tests and add tests for Server.prototype.error

  • Loading branch information...
1 parent ec7bcbb commit 4dcd85f83cf59de370f220455f9cb02ef8c93bfb @tedeh committed Oct 7, 2013
Showing with 33 additions and 11 deletions.
  1. +29 −4 test/server.test.js
  2. +4 −7 test/support/client-server.js
View
@@ -58,19 +58,44 @@ describe('jayson server instance', function() {
server.hasMethod(methodName).should.be.false;
});
+ // change server instance error message, request erroring method, assert error message changed
it('should allow standard error messages to be changed', function(done) {
- var newMsg = 'Parse Error!';
- server.errorMessages[jayson.server.errors.PARSE_ERROR] = newMsg;
+ var newMsg = server.errorMessages[jayson.server.errors.PARSE_ERROR] = 'Parse Error!';
server.call('invalid request', function(err, response) {
should.exist(err);
should.exist(err.error);
should.not.exist(response);
- err.error.code.should.equal(jayson.server.errors.PARSE_ERROR);
- err.error.message.should.be.a('string').and.equal(newMsg);
+ err.error.should.have.property('code', jayson.server.errors.PARSE_ERROR);
+ err.error.should.have.property('message', newMsg);
done();
});
});
+ describe('error()', function() {
+
+ it('should not make an error out of an invalid code', function() {
+ var error = server.error('invalid_code');
+ should.exist(error);
+ error.should.have.property('code', jayson.Server.errors.INTERNAL_ERROR);
+ });
+
+ it('should fill in the error message if not passed one', function() {
+ var code = jayson.Server.errors.INVALID_PARAMS;
+ var error = server.error(code);
+ should.exist(error);
+ error.should.have.property('code', code);
+ error.should.have.property('message', jayson.Server.errorMessages[code]);
+ });
+
+ it('should add a data member if specified', function() {
+ var data = {member: 1};
+ var code = jayson.Server.errors.INVALID_PARAMS;
+ var error = server.error(code, null, data);
+ error.should.have.property('data', data);
+ });
+
+ });
+
describe('event handlers', function() {
(function() {
@@ -19,8 +19,7 @@ exports.clientRequest = function(context) {
var client = context.client;
var a = 11, b = 12;
client.request('add', [a, b], function(err, error, result) {
- should.not.exist(err);
- should.not.exist(error);
+ if(err || error) return done(err || error);
should.exist(result);
result.should.equal(a + b);
done();
@@ -35,10 +34,8 @@ exports.clientError = function(context) {
should.not.exist(err);
should.not.exist(result);
should.exist(error);
- should.exist(error.code);
- should.exist(error.message);
- error.message.should.equal('An error message');
- error.code.should.equal(-1000);
+ error.should.have.property('message', 'An error message');
+ error.should.have.property('code', -1000);
done();
});
};
@@ -54,7 +51,7 @@ exports.clientReviveReplace = function(context) {
should.not.exist(error);
should.exist(result);
result.should.be.instanceof(Counter).and.not.equal(instance, 'Not the same object');
- result.count.should.equal(a + b);
+ result.should.have.property('count', a + b);
done();
});
};

0 comments on commit 4dcd85f

Please sign in to comment.