Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[tests] [refactor] Improved test coverage for executing Resource.meth…

…od functions. Merged Class and Instance tests into one macro.
  • Loading branch information...
commit 3799bbd05cb63bf67de8f4d2eea0857dd3189247 1 parent 837c6ca
@Marak Marak authored
Showing with 102 additions and 94 deletions.
  1. +102 −94 test/resourceful-methods-test.js
View
196 test/resourceful-methods-test.js
@@ -14,8 +14,16 @@ function describeResourceWithMethods(){
}
return 'poked!';
}
- function talk () {
- console.log('talk');
+ function talk (text, target, callback) {
+ var result = {
+ text: text,
+ target: target,
+ status: 200
+ }
+ if (callback) {
+ return callback(null, result);
+ }
+ return result;
};
function fire (options, callback) {
var result = {
@@ -32,57 +40,97 @@ function describeResourceWithMethods(){
r.method('poke', poke);
r.method('fire', fire, {
- properties: {
- "power": {
- "type": "number",
- "default": 1,
- "required": true
- },
- "direction": {
- "type": "string",
- "enum": ["up", "down", "left", "right"],
- "required": true,
- "default": "up"
- }
+ "description": "fires a lazer at a certain power and direction",
+ "properties": {
+ "options": {
+ "power": {
+ "type": "number",
+ "default": 1,
+ "required": true
+ },
+ "direction": {
+ "type": "string",
+ "enum": ["up", "down", "left", "right"],
+ "required": true,
+ "default": "up"
+ },
+ "callback": {
+ "type": "function",
+ "required": false
+ }
+ }
}});
- r.method('talk', talk, { properties: {
- "text": {
- "type": "string",
- "required": true
+ r.method('talk', talk, {
+ "description": "echos back a string",
+ "properties": {
+ "text": {
+ "type": "string",
+ "required": true
+ }
}
- }});
+ });
return r;
}
-vows.describe('resourceful').addVows({ // property
- "A Resource with methods": {
- topic: function () {
- var r = describeResourceWithMethods();
- return r;
- },
- "adds them to `Resource.methods`": function (R) {
- assert.equal(Object.keys(R.methods).length, 3);
- assert.include(R.methods, 'fire');
- assert.include(R.methods, 'talk');
- },
- "should be able to fire simple method with callback" : function (R) {
+function resourceTest (instance) {
+ return {
+ topic: function () {
+ var r = describeResourceWithMethods();
+ //
+ // Remarks: tests are run on both the Class and the Instance
+ //
+ if(instance) {
+ return new r();
+ } else {
+ return r;
+ }
+ },
+ "adds them to `Resource`": function (R) {
+ assert.isFunction(R.fire);
+ assert.isFunction(R.talk);
+ },
+ "should be able to fire method with no schema and callback" : function (R) {
R.poke(function(err, result){
assert.isNull(err);
assert.equal(result, 'poked!');
});
},
- "should be able to fire simple method with no callback" : function (R) {
+ "should be able to fire method with no schema and no callback" : function (R) {
var result = R.poke();
assert.equal(result, 'poked!');
},
- "should error on firing schema method with bad arguments and callback" : function (R) {
+ "should be able to fire method with one argument and a callback" : function (R) {
+ R.talk("hello", "marak", function(err, result){
+ assert.isNull(err);
+ assert.isObject(result);
+ });
+ },
+ "should be able to fire method with two arguments and no callback" : function (R) {
+ var result = R.talk("hello", "marak");
+ assert.isObject(result, result);
+ },
+ "should be able to fire method method with oneargument and callback" : function (r) {
+ r.talk("hello", function(err, result){
+ assert.isNull(err);
+ });
+ },
+ "should be able to fire method with single arguments and no callback" : function (r) {
+ var result;
+ try {
+ result = r.talk("hello");
+ } catch (err) {
+ assert.isNull(err);
+ return;
+ }
+ },
+ "should error on firing method with invalid signature arguments and callback" : function (R) {
R.fire(function(err, result){
assert.isNotNull(err);
});
},
- "should error on firing schema method with bad arguments and no callback" : function (R) {
+ "should error on firing method with signature and no callback" : function (R) {
try {
var result = R.fire();
} catch (err) {
@@ -91,7 +139,7 @@ vows.describe('resourceful').addVows({ // property
}
assert.equal(true, false)
},
- "should be able to fire schema method with good arguments and callback" : function (R) {
+ "should be able to fire method with good signature and callback" : function (R) {
R.fire({ power: 9001, direction: "up" }, function(err, result){
assert.isNull(err);
assert.isObject(result);
@@ -100,7 +148,7 @@ vows.describe('resourceful').addVows({ // property
assert.equal(result.direction, 'up');
});
},
- "should be able to fire schema method with good arguments and no callback" : function (R) {
+ "should be able to fire method with good signature and no callback" : function (R) {
var result;
try {
result = R.fire({ power: 9001, direction: "up" });
@@ -113,65 +161,25 @@ vows.describe('resourceful').addVows({ // property
assert.equal(result.power, 9001);
assert.equal(result.direction, 'up');
},
- "When instantiated": {
- topic: function (R) {
- var r = new(R)({ name: 'Larry', type: 'Dragon' });
- return r;
- },
- "should respond to toJSON()": function (r) {
- assert.isObject(r.toJSON());
- },
- "should return the attributes, when `Object.keys` is called": function (r) {
- var keys = Object.keys(r);
- assert.include(keys, 'fire');
- assert.include(keys, 'talk');
- },
- "should be able to fire simple method with callback" : function (r) {
- r.poke(function(err, result){
- assert.isNull(err);
- assert.equal(result, 'poked!');
- });
- },
- "should be able to fire simple method with no callback" : function (r) {
- var result = r.poke();
- assert.equal(result, 'poked!');
- },
- "should error on firing schema method with bad arguments and callback" : function (r) {
- r.fire(function(err, result){
- assert.isNotNull(err);
- });
- },
- "should error on firing schema method with bad arguments and no callback" : function (r) {
- try {
- var result = r.fire();
- } catch (err) {
- assert.isNotNull(err);
- return;
- }
- assert.equal(true, false)
- },
- "should be able to fire schema method with good arguments and callback" : function (r) {
- r.fire({ power: 9001, direction: "up" }, function(err, result){
- assert.isNull(err);
- assert.isObject(result);
- assert.equal(result.status, 'fired');
- assert.equal(result.power, 9001);
- assert.equal(result.direction, 'up');
- });
- },
- "should be able to fire schema method with good arguments and no callback" : function (r) {
- var result;
- try {
- result = r.fire({ power: 9001, direction: "up" });
- } catch (err) {
- assert.isNull(err);
- return;
- }
- assert.isObject(result);
- assert.equal(result.status, 'fired');
- assert.equal(result.power, 9001);
- assert.equal(result.direction, 'up');
+ "should error when firing methods with invalid schema values and no callback" : function (r) {
+ var result;
+ try {
+ result = r.fire({ power: 9001, direction: "side-ways" });
+ } catch (err) {
+ assert.isNotNull(err);
+ return;
}
+ assert.equal(false, true);
+ },
+ "should error when firing methods with invalid schema values with callback" : function (r) {
+ r.fire({ power: 9001, direction: "side-ways" }, function(err, result){
+ assert.isNotNull(err);
+ });
}
}
+}
+
+vows.describe('resourceful').addVows({
+ "A Resource with methods": resourceTest(),
+ "An instance of that Resource": resourceTest(true),
}).export(module);
Please sign in to comment.
Something went wrong with that request. Please try again.