jsmock
Advanced tools
Comparing version 0.2.0 to 0.3.0
@@ -8,4 +8,3 @@ 'use strict'; | ||
/** @class Action | ||
* | ||
/** Class specifying action to be executed on mocked function calls. | ||
*/ | ||
@@ -18,3 +17,8 @@ class Action { | ||
* @param {Any} action | ||
* If provided action is a function it will be executed with arguments provided to | ||
* execute method. Parameter of any other type will produce action returning it | ||
* upon execution. | ||
* @param {Number} counter | ||
* Expected number of action execution times. Negative value will create action | ||
* that can be executed any number of times. | ||
* | ||
@@ -52,2 +56,6 @@ * @throws {TypeErrpr} | ||
/** Determines if action has been executed expected number of times. | ||
* | ||
* @returns {Boolean} | ||
*/ | ||
validate() { | ||
@@ -57,2 +65,7 @@ return this.counter <= 0; | ||
/** Determines if action can be executed. | ||
* | ||
* @returns {Boolean} | ||
* Returns true if action can be executed, false otherwise. | ||
*/ | ||
ready() { | ||
@@ -78,5 +91,9 @@ return this.counter != 0; | ||
/** | ||
/** Specifies expected execution count. Equivalent of providing | ||
* execution count in Action @see constructor | ||
* | ||
* @param {Number} count | ||
* @param {Number} count | ||
* @throws {Error} | ||
* Throws exception if given number is equal 0. | ||
* | ||
* @returns {Expectation} | ||
@@ -83,0 +100,0 @@ * Returns expectation owning current action for chaining. |
@@ -118,2 +118,16 @@ 'use strict'; | ||
/** Adds a new action from provided arguments and adds it to | ||
* expectation action list. Newly created Action has expected | ||
* execution count set to 2. | ||
* | ||
* @param {*} action | ||
* Parameter passed for Action constructor | ||
* @returns {Expectation} | ||
* Returns current instance of the expectation for chaining. | ||
*/ | ||
willTwice(action) { | ||
this.actions.push(new Action(this, action, 2)); | ||
return this; | ||
} | ||
/** Adds a new action from provided arguments and adds it to | ||
* expectation action list. Newly created Action has unbounded | ||
@@ -120,0 +134,0 @@ * execution count. |
'use strict'; | ||
class Anything { | ||
}; | ||
const _ = require('lodash'); | ||
class Anything {}; | ||
const ANYTHING = new Anything(); | ||
@@ -7,0 +8,0 @@ |
@@ -20,14 +20,6 @@ 'use strict'; | ||
this.originals[functionName] = object[functionName]; | ||
object[functionName] = this.unexpectedCall.bind(this, functionName); | ||
object[functionName] = this.mockedCall.bind(this, functionName); | ||
} | ||
} | ||
/** Default bind for all functions of mocked object | ||
* @param {String} functionName | ||
* Name of the function being called | ||
*/ | ||
unexpectedCall(functionName) { | ||
throw new Error('Unexpected call of ' + functionName); | ||
} | ||
/** Bind for a function of a mocked object having at | ||
@@ -38,5 +30,8 @@ * least one expectation properly set | ||
*/ | ||
expectedCall(functionName) { | ||
mockedCall(functionName) { | ||
const args = Array.from(arguments).slice(1); | ||
const expectations = this.expectations[functionName]; | ||
if(!expectations || expectations.length === 0) { | ||
throw new Error('Unexpected call of ' + functionName); | ||
} | ||
const idx = _.findIndex(expectations, exp => exp.isMatching(args) && !exp.isSaturated()); | ||
@@ -59,6 +54,2 @@ if(idx < 0) { | ||
this.expectations[functionName] = this.expectations[functionName] || []; | ||
let expectations = this.expectations[functionName]; | ||
if(expectations.length === 0) { | ||
this.mocked[functionName] = this.expectedCall.bind(this, functionName); | ||
} | ||
let expectation = new Expectation(Array.from(arguments).slice(1)); | ||
@@ -65,0 +56,0 @@ this.expectations[functionName].push(expectation); |
@@ -12,4 +12,9 @@ 'use strict'; | ||
/** Spy is a simplified version of a Mock. It wraps all functions of provided | ||
* object with some additional code that enables storage of passed arguments | ||
* and returned values. Spy objects allow to perform post execution validation | ||
* of used interfaces with test assertions. | ||
*/ | ||
class Spy { | ||
/** | ||
/** Creates a new spy over given object. | ||
* | ||
@@ -32,4 +37,3 @@ * @param {Object} object | ||
/** | ||
* | ||
/** Removes all the tooling code from spyed object. | ||
*/ | ||
@@ -36,0 +40,0 @@ cleanup() { |
{ | ||
"name": "jsmock", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "Mocking framework for javascript", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
31663
809