Permalink
Browse files

update docs, add coverage

  • Loading branch information...
vilmibm committed Sep 3, 2012
1 parent fb2088e commit 352a36ebee9305e33e369e93af64f8d290044193
Showing with 40 additions and 7 deletions.
  1. +11 −1 README.md
  2. +3 −5 lib/akeley.js
  3. +1 −1 package.json
  4. +25 −0 tests/test.js
View
@@ -42,10 +42,20 @@ experimentation until further notice.
## just make a watched function
+ // set a return value
var mockf = Mock.create_func({return_value:5});
mockf(): // 5
mockf.called // true
- // etc
+
+ // wrap a real function
+ var mockf = Mock.create_func({
+ func: some_useful_real_func
+ });
+
+ // cause side effects
+ var mockf = Mock.create_func({
+ side_effect: function() { throw 'error' }
+ });
## create a nested structure
View
@@ -39,11 +39,9 @@ Mock.create_func = function(opts) {
function_spy.calls += 1;
function_spy.args.push(args);
function_spy.called = true;
- if (func) { return func.apply(this, args); }
- else {
- side_effect();
- return return_value;
- }
+ side_effect.apply(this, args);
+ if (func) { return func.apply(this, args) }
+ else { return return_value }
};
['calls', 'args', 'called'].forEach(function(k) {
View
@@ -1,6 +1,6 @@
{
"name": "akeley",
- "version": "0.3.1",
+ "version": "0.3.2",
"author": "nathaniel k smith <nathanielksmith@gmail.com>",
"description": "a mocking utility library",
"main": "./lib/akeley",
View
@@ -104,6 +104,17 @@ exports.test_mock_function = {
test.throws(mock_f, 'error', 'saw error');
test.done();
},
+ test_side_effect_and_func: function(test) {
+ var affected = 0;
+ var mockf = Mock.create_func({
+ func: function() { return 5 },
+ side_effect: function() { affected += 1}
+ });
+ var ret = mockf();
+ test.equal(affected, 1, 'side effect happened');
+ test.equal(ret, 5, 'func called');
+ test.done();
+ },
test_return_value: function(test) {
var mock_f = Mock.create_func({return_value: 5});
test.equal(mock_f(), 5, 'saw return value');
@@ -132,3 +143,17 @@ exports.test_mock_function = {
test.done();
}
};
+
+exports.test_create_nested = {
+ test_empty_target: function(test) {
+ var result = Mock.create_nested_obj({}, ['hi', 'there', 'how']);
+ test.ok(result.hi.there.how, 'nested properly');
+ test.done();
+ },
+ test_nonempty_target: function(test) {
+ var result = Mock.create_nested_obj({a:'b'}, ['hi', 'there', 'how']);
+ test.ok(result.hi.there.how, 'nested properly');
+ test.equal(result.a, 'b', 'preserved target');
+ test.done();
+ }
+};

0 comments on commit 352a36e

Please sign in to comment.