Skip to content

Commit

Permalink
add a reset method to wrapped functions
Browse files Browse the repository at this point in the history
  • Loading branch information
vilmibm committed Sep 7, 2012
1 parent 352a36e commit efbad6c
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
7 changes: 2 additions & 5 deletions README.md
Expand Up @@ -2,11 +2,6 @@

_being a mocking utility_

## status

very much a work in progress. not recommended for any use outside of
experimentation until further notice.

## mock something out

var Mock = require('akeley').Mock
Expand Down Expand Up @@ -40,6 +35,8 @@ experimentation until further notice.
mock_akeley.think.calls; // 2
mock_akeley.think.args; // [ ['i hate the mi-go'], ['i love the mi-go'] ]

mock_akeley.think.reset(); // reset called, calls, args

## just make a watched function

// set a return value
Expand Down
6 changes: 6 additions & 0 deletions lib/akeley.js
Expand Up @@ -44,6 +44,12 @@ Mock.create_func = function(opts) {
else { return return_value }
};

wrapped.reset = function() {
function_spy.called = false;
function_spy.calls = 0;
function_spy.args = [];
};

['calls', 'args', 'called'].forEach(function(k) {
Object.defineProperty(wrapped, k, {
get: function() { return function_spy[k]; }
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "akeley",
"version": "0.3.2",
"version": "0.4.0",
"author": "nathaniel k smith <nathanielksmith@gmail.com>",
"description": "a mocking utility library",
"main": "./lib/akeley",
Expand Down
11 changes: 10 additions & 1 deletion tests/test.js
Expand Up @@ -29,7 +29,6 @@ exports.test_object_spec = {
test.equal(mock_cat.vitals.appearance.legs, 4, 'legs set');
test.deepEqual(mock_cat.vitals.appearance.colours, ['orange', 'black', 'brown'], 'colours set');
test.done()

},
test_functions: function(test) {
var cat = {
Expand Down Expand Up @@ -140,6 +139,16 @@ exports.test_mock_function = {
mock_f();
test.ok(mock_f.called, 'saw it was called');

test.done();
},
test_reset: function(test) {
var mock_f = Mock.create_func();
mock_f(); mock_f(); mock_f();
test.equal(mock_f.calls, 3, 'sanity: saw three calls');
mock_f.reset();
test.equal(mock_f.calls, 0, 'reset call count');
test.equal(mock_f.called, false, 'reset called boolean');
test.deepEqual(mock_f.args, [], 'reset args');
test.done();
}
};
Expand Down

0 comments on commit efbad6c

Please sign in to comment.