Comparing version 0.3.3 to 0.3.4
/** | ||
The MIT License | ||
Copyright (c) 2011 Arunoda Susiripala | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
@@ -13,6 +13,6 @@ of this software and associated documentation files (the "Software"), to deal | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
@@ -28,17 +28,21 @@ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
if (typeof Buffer === 'undefined') { | ||
Buffer = function Buffer() {}; | ||
} | ||
function NodeMock(methodName) { | ||
var self = this; | ||
var currentMockFunction = null; | ||
var entries = {}; | ||
var mockFunction = function(method, avoid) { | ||
function getValidEntry(args) { | ||
//Iterate over to find a entry matching argument list | ||
for(var index in entries[method]) { | ||
var entry = entries[method][index]; | ||
var doArgsMatch = entry.executed === false && entry.args && deepObjectCheck(entry.args, args); | ||
@@ -54,13 +58,13 @@ var areArgsValid = entry.executed === false && entry.checkArgs && entry.checkArgs.apply(this, args); | ||
} | ||
return false; | ||
} | ||
return function() { | ||
//check for argument length | ||
var entry; | ||
if(!self[method]) { | ||
throw new Error("Mock function '" + method + "()' is not defined"); | ||
} else if(!(entry = getValidEntry(arguments))) { | ||
@@ -80,3 +84,3 @@ | ||
expected += "\n"; | ||
if(alreadyExecuted) { | ||
@@ -86,10 +90,10 @@ throw new Error('method: ' + method + getParamString(arguments) + ' already executed'); | ||
throw new Error( | ||
"Arguments content passed: " + getParamString(arguments) + | ||
"Arguments content passed: " + getParamString(arguments) + | ||
" is not tally with takesAll function or expected arguments: " + expected + " in method: '" + method + "()'"); | ||
} | ||
} else if(entry.shouldFail) { | ||
throw new Error("You should not call: '" + method+ "()' with params: " + getParamString(arguments) + " on this object"); | ||
} | ||
//calling the callback | ||
@@ -99,3 +103,3 @@ if(entry.callbackIndex !== null) { | ||
entry.callback = func; | ||
if(entry.callbackArgs) { | ||
@@ -109,4 +113,4 @@ if(typeof(func) == "function") { | ||
} | ||
if(entry.returnsF) | ||
if(entry.returnsF) | ||
return(entry.returnsF.apply(this, arguments)); | ||
@@ -117,3 +121,3 @@ else | ||
}; | ||
this.takes = function() { | ||
@@ -126,3 +130,3 @@ var entry = getCurrentEntry(); | ||
this.takesF = function(checkArgsFunction) { | ||
if (typeof(checkArgsFunction) !== 'function') | ||
if (typeof(checkArgsFunction) !== 'function') | ||
throw new Error("Error: argument is not a function."); | ||
@@ -137,4 +141,4 @@ | ||
return(this.takesF(function() { return true; })); | ||
}; | ||
}; | ||
this.times = function(expected) { | ||
@@ -147,3 +151,3 @@ var entry = getCurrentEntry(); | ||
}; | ||
this.returns = function(value) { | ||
@@ -160,3 +164,3 @@ var entry = getCurrentEntry(); | ||
}; | ||
this.ctrl = function(index, obj) { | ||
@@ -166,3 +170,3 @@ var entry = getCurrentEntry(); | ||
entry.callbackArgs = false; | ||
obj.trigger = function() { | ||
@@ -175,11 +179,11 @@ if(entry.callback) { | ||
}; | ||
return this; | ||
}; | ||
this.calls = function() { | ||
if(typeof(arguments[0]) == "number") { | ||
var entry = entries[currentMockFunction][entries[currentMockFunction].length - 1]; | ||
entry.callbackIndex = arguments[0]; | ||
@@ -189,6 +193,6 @@ if(arguments[1] && (arguments[1] instanceof Array)) { | ||
} | ||
return this; | ||
} else if(arguments[0] instanceof Object) { | ||
} else { | ||
@@ -216,3 +220,3 @@ throw new Error("First arg of the calls() should be the index of the callback"); | ||
}; | ||
/* jshint -W083 */ | ||
@@ -236,12 +240,12 @@ this.assert = function() { | ||
}; | ||
//Assign the mocking function | ||
this.mock = function(method) { | ||
if(method) { | ||
//let the current mocking method be this | ||
currentMockFunction = method; | ||
if(!this[method]) { | ||
entries[currentMockFunction] = []; | ||
@@ -251,3 +255,3 @@ //assign the mock method | ||
} | ||
addEntry({ | ||
@@ -263,6 +267,6 @@ args: [], | ||
} | ||
return this; | ||
}; | ||
/** | ||
@@ -284,3 +288,3 @@ * After this call when someone calls on this this object is'll | ||
currentMockFunction = null; | ||
var bypass = { | ||
@@ -307,3 +311,3 @@ 'takes': true, | ||
} | ||
} | ||
} | ||
}; | ||
@@ -313,3 +317,3 @@ | ||
this.ignore = function(method) { | ||
this[method] = function() {}; | ||
@@ -326,3 +330,3 @@ }; | ||
} | ||
function getCurrentEntry() { | ||
@@ -335,5 +339,5 @@ return entries[currentMockFunction][entries[currentMockFunction].length - 1]; | ||
} | ||
function getParamString(params) { | ||
var paramString = "("; | ||
@@ -350,3 +354,3 @@ for(var index in params) { | ||
} | ||
if(params[0]) { | ||
@@ -358,12 +362,17 @@ return paramString.substring(0, paramString.length -2) + ")"; | ||
} | ||
function deepObjectCheck(expected, actual) { | ||
var exIsBuffer = expected instanceof Buffer; | ||
if(expected && actual && (expected.length != actual.length)) return false; | ||
for(var key in expected) { | ||
if(exIsBuffer && key === 'offset') { | ||
continue; | ||
} | ||
var actualType = typeof(actual[key]); | ||
var expectedType = typeof(expected[key]); | ||
if(actualType != expectedType) return false; | ||
@@ -378,9 +387,9 @@ if(actualType == "function") { | ||
} | ||
return true; | ||
} | ||
//initialize the mocking | ||
this.mock(methodName); | ||
} | ||
@@ -403,3 +412,3 @@ | ||
}; | ||
exports.fail = function() { | ||
@@ -406,0 +415,0 @@ return new NodeMock(); |
{ | ||
"name": "nodemock", | ||
"version": "0.3.3", | ||
"version": "0.3.4", | ||
"directories": { | ||
@@ -13,3 +13,7 @@ "test": "test" | ||
"Curtis Schlak <realistschuckle@gmail.com>", | ||
"Oscar Renalias <oscar@renalias.net>" | ||
"Oscar Renalias <oscar@renalias.net>", | ||
"Aaron Fay <aaron.j.fay@gmail.com>", | ||
"Dominic Tarr <dominic.tarr@gmail.com>", | ||
"Daniel @0xDB", | ||
"Gabriel Falcão <gabriel@nacaolivre.org>" | ||
], | ||
@@ -16,0 +20,0 @@ "scripts": { |
/* | ||
The MIT License | ||
Copyright (c) 2011 Arunoda Susiripala | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
@@ -13,6 +13,6 @@ of this software and associated documentation files (the "Software"), to deal | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
@@ -28,6 +28,5 @@ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
//NodeMock argument check | ||
var nm = require("../lib/nodemock"); | ||
exports.mocksHaveNames = function(test) { | ||
exports['mocks can have names which get reported in error messages'] = function(test) { | ||
var mockName = 'myFabulousMock', | ||
@@ -45,163 +44,185 @@ mock = nm.named(mockName).mock('foo').takes(1); | ||
exports.testArgumentSimple = function(test) { | ||
var mock = nm.mock("foo").takes(10, "Hello", true, [1, 4, 5], {"a": "aa", "b": "bb"}); | ||
test.doesNotThrow(function(){ | ||
mock.foo(10, "Hello", true, [1, 4, 5], {"a": "aa", "b": "bb"}); | ||
}); | ||
test.throws(function() { | ||
mock.foo(11, "Hello", true, [1, 4, 5], {"a": "aa", "b": "bb"}); | ||
}); | ||
test.throws(function() { | ||
mock.foo(10, "Hello1", true, [1, 4, 5], {"a": "aa", "b": "bb"}); | ||
}); | ||
test.throws(function() { | ||
mock.foo(10, "Hello", false, [1, 4, 8], {"a": "aa", "b": "bb"}); | ||
}); | ||
test.throws(function() { | ||
mock.foo(10, "Hello", true, [1, 4, 5], {"a": "aa", "b": "gh"}); | ||
}); | ||
test.throws(function() { | ||
mock.foo(10, "Hello", true, [1, 4, 5], {"a": "aa", "c": "gh"}); | ||
}); | ||
test.done(); | ||
}; | ||
exports['mocked functions pattern match on'] = { | ||
setUp: function(cb) { | ||
this.mock = nm.mock('foo') | ||
.takes(10, "Hello", true, [1, 4, 5], {"a": "aa", "b": "bb"}); | ||
cb(); | ||
} | ||
exports.testWithJustReturn = function(test) { | ||
var mock = nm.mock("foo").returns(false); | ||
test.ok(!mock.foo()); | ||
test.done(); | ||
}; | ||
, numbers: function(test) { | ||
var mock = this.mock; | ||
test.doesNotThrow(function() { | ||
mock.foo(10, "Hello", true, [1, 4, 5], {"a": "aa", "b": "bb"}); | ||
}); | ||
test.throws(function() { | ||
mock.foo(-1, "Hello", true, [1, 4, 5], {"a": "aa", "b": "bb"}); | ||
}); | ||
test.done(); | ||
} | ||
exports.testArgumentsDeep = function(test) { | ||
var mock = nm.mock("foo").takes([10, 20, {a: "aa", b: [10, 20]}]); | ||
test.doesNotThrow(function() { | ||
mock.foo([10, 20, {a: "aa", b: [10, 20]}]); | ||
}); | ||
test.throws(function() { | ||
mock.foo([10, 21, {a: "aa", b: [10, 20]}]); | ||
}); | ||
test.throws(function() { | ||
mock.foo([10, 20, {a: "aa3", b: [10, 20]}]); | ||
}); | ||
test.throws(function() { | ||
mock.foo([10, 20, {a: "aa", b: [10, 20, 30]}]); | ||
}); | ||
test.done(); | ||
}; | ||
, strings: function(test) { | ||
var mock = this.mock; | ||
test.doesNotThrow(function() { | ||
mock.foo(10, "Hello", true, [1, 4, 5], {"a": "aa", "b": "bb"}); | ||
}); | ||
test.throws(function() { | ||
mock.foo(10, "WORLD", true, [1, 4, 5], {"a": "aa", "b": "bb"}); | ||
}); | ||
test.done(); | ||
} | ||
//Nodemock Return Check | ||
, booleans: function(test) { | ||
var mock = this.mock; | ||
test.doesNotThrow(function() { | ||
mock.foo(10, "Hello", true, [1, 4, 5], {"a": "aa", "b": "bb"}); | ||
}); | ||
test.throws(function() { | ||
mock.foo(10, "Hello", false, [1, 4, 5], {"a": "aa", "b": "bb"}); | ||
}); | ||
test.done(); | ||
} | ||
exports.testReturn = function(test) { | ||
var mock = nm.mock("foo").returns(10); | ||
test.equal(mock.foo(), 10); | ||
test.done(); | ||
, arrays: function(test) { | ||
var mock = this.mock; | ||
test.doesNotThrow(function() { | ||
mock.foo(10, "Hello", true, [1, 4, 5], {"a": "aa", "b": "bb"}); | ||
}); | ||
test.throws(function() { | ||
mock.foo(10, "Hello", true, [1, 5, 4], {"a": "aa", "b": "bb"}); | ||
}); | ||
test.done(); | ||
} | ||
, objects: function(test) { | ||
var mock = this.mock; | ||
test.doesNotThrow(function() { | ||
mock.foo(10, "Hello", true, [1, 4, 5], {"a": "aa", "b": "bb"}); | ||
}); | ||
test.throws(function() { | ||
mock.foo(10, "Hello", true, [1, 4, 5], {"c": "aa", "b": "aa"}); | ||
}); | ||
test.done(); | ||
} | ||
, 'nested objects': function(test) { | ||
var mock = nm.mock('foo').takes([10, 20, {a: "aa", b: [10, 20]}]); | ||
test.doesNotThrow(function() { | ||
mock.foo([10, 20, {a: "aa", b: [10, 20]}]); | ||
}); | ||
test.throws(function() { | ||
mock.foo([10, 21, {a: "aa", b: [10, 20]}]); | ||
}); | ||
test.throws(function() { | ||
mock.foo([10, 20, {a: "aa3", b: [10, 20]}]); | ||
}); | ||
test.throws(function() { | ||
mock.foo([10, 20, {a: "aa", b: [10, 20, 30]}]); | ||
}); | ||
test.done(); | ||
} | ||
, 'differing arguments': function(test) { | ||
var mock = nm.mock("foo").takes(10, 20).returns(30); | ||
mock.mock("foo").takes(10, 30).returns(40); | ||
test.doesNotThrow(function() { | ||
test.ok(mock.foo(10, 20) == 30); | ||
test.ok(mock.foo(10, 30) == 40); | ||
}); | ||
test.throws(function() { | ||
mock.foo(10); | ||
mock.foo(10, 20); | ||
mock.bar(10, 30); | ||
}); | ||
test.done(); | ||
} | ||
}; | ||
//Nodemock Callback Tests | ||
exports.testCallbackCorrect = function(test) { | ||
var mock = nm.mock("foo").takes(10, function(){}).calls(1, [10, 20]); | ||
test.expect(2); | ||
mock.foo(10, function(a, b) { | ||
test.equals(a, 10); | ||
test.equals(b, 20); | ||
exports['mocked functions will return a value for no arguments'] = function(test) { | ||
[1, '2', [3], {four: 4}, null, undefined].forEach(function(value) { | ||
var mock = nm.mock('foo').returns(value); | ||
test.equal(mock.foo(), value); | ||
}); | ||
test.done(); | ||
}; | ||
exports.testCallbackNoArgsDefined = function(test) { | ||
var mock = nm.mock("foo").calls(1, [10, 20]); | ||
exports['mocked functions with a callback'] = { | ||
'passes parameters to callback': function(test) { | ||
var empty = function() {} | ||
, mock = nm.mock('foo').takes(10, empty).calls(1, [10, 20]) | ||
; | ||
mock.foo(10, function(a, b) { | ||
test.equals(a, 10); | ||
test.equals(b, 20); | ||
test.done(); | ||
}); | ||
} | ||
test.throws(function() { | ||
, 'cannot invoke without takes': function(test) { | ||
var mock = nm.mock('foo').calls(1, [10, 20]) | ||
; | ||
test.throws(function() { | ||
mock.foo(10, function() {}); | ||
}); | ||
test.done(); | ||
} | ||
, 'callback can get surplus arguments': function(test) { | ||
var empty = function() {} | ||
, mock = nm.mock('foo').takes(10, empty).calls(1, [10, 20, 30, 40]) | ||
; | ||
mock.foo(10, function(a, b) { | ||
test.equals(a, 10); | ||
test.equals(b, 20); | ||
test.equals(arguments[2], 30); | ||
test.equals(arguments[3], 40); | ||
test.done(); | ||
}); | ||
}); | ||
test.done(); | ||
}; | ||
} | ||
exports.testCallbackSurplusArgs = function(test) { | ||
var mock = nm.mock("foo").takes(10, function(){}).calls(1, [10, 20, 30, 40]); | ||
test.expect(2); | ||
mock.foo(10, function(a, b) { | ||
test.equals(a, 10); | ||
test.equals(b, 20); | ||
}); | ||
test.done(); | ||
}; | ||
, 'callback can get sparse arguments': function(test) { | ||
var empty = function() {} | ||
, mock = nm.mock('foo').takes(10, empty).calls(1, [10]) | ||
; | ||
mock.foo(10, function(a, b) { | ||
test.equals(a, 10); | ||
test.equals(b, undefined); | ||
test.done(); | ||
}); | ||
} | ||
exports.testCallbackLessArgs = function(test) { | ||
var mock = nm.mock("foo").takes(10, function(){}).calls(1, [10]); | ||
test.expect(2); | ||
mock.foo(10, function(a, b) { | ||
test.equals(a, 10); | ||
test.equals(b, undefined); | ||
}); | ||
test.done(); | ||
}; | ||
, 'callback can get no arguments': function(test) { | ||
var empty = function() {} | ||
, mock = nm.mock('foo').takes(10, empty).calls(1, []) | ||
; | ||
mock.foo(10, function(a, b) { | ||
test.equals(a, undefined); | ||
test.equals(b, undefined); | ||
test.done(); | ||
}); | ||
} | ||
exports.testCallbackNoArgs = function(test) { | ||
var mock = nm.mock("foo").takes(10, function(){}).calls(1, []); | ||
test.expect(2); | ||
mock.foo(10, function(a, b) { | ||
test.equals(a, undefined); | ||
test.equals(b, undefined); | ||
}); | ||
test.done(); | ||
}; | ||
, 'callback with null gets undefined arguments': function(test) { | ||
var empty = function() {} | ||
, mock = nm.mock('foo').takes(10, empty).calls(1, null) | ||
; | ||
mock.foo(10, function(a, b) { | ||
test.equals(a, undefined); | ||
test.equals(b, undefined); | ||
test.done(); | ||
}); | ||
} | ||
exports.testCallbackNullArgs = function(test) { | ||
var mock = nm.mock("foo").takes(10, function(){}).calls(1, null); | ||
test.expect(2); | ||
mock.foo(10, function(a, b) { | ||
test.equals(a, undefined); | ||
test.equals(b, undefined); | ||
}); | ||
test.done(); | ||
}; | ||
, 'callback works with no arguments': function(test) { | ||
var empty = function() {} | ||
, mock = nm.mock('foo').takes(10, empty).calls(1) | ||
; | ||
mock.foo(10, function(a, b) { | ||
test.equals(a, undefined); | ||
test.equals(b, undefined); | ||
test.done(); | ||
}); | ||
} | ||
} | ||
exports.testCallbackWithoutArgs = function(test) { | ||
var mock = nm.mock("foo").takes(10, function(){}).calls(1); | ||
test.expect(2); | ||
mock.foo(10, function(a, b) { | ||
test.equals(a, undefined); | ||
test.equals(b, undefined); | ||
}); | ||
test.done(); | ||
}; | ||
exports.testMockAgain = function(test) { | ||
exports['mocked functions are strict by default'] = function(test) { | ||
var mock = nm.mock("foo").takes(10, 20).returns(30); | ||
@@ -211,3 +232,2 @@ test.doesNotThrow(function() { | ||
}); | ||
mock.mock("bar").takes(10).returns(40); | ||
@@ -217,3 +237,2 @@ test.doesNotThrow(function() { | ||
}); | ||
test.throws(function() { | ||
@@ -225,4 +244,3 @@ test.equal(mock.foo(10, 20), 30); | ||
exports.testFailNoAnyMockMethod = function(test) { | ||
exports['mocked functions with fail always throw errors'] = function(test) { | ||
var mock = nm.fail(); | ||
@@ -235,4 +253,3 @@ test.throws(function() { | ||
exports.testFailOneMockMethod = function(test) { | ||
exports['mock can have both fail and non-fail functions'] = function(test) { | ||
var mock = nm.mock("foo").fail(); | ||
@@ -243,3 +260,2 @@ mock.mock("bar").takes(10, 20); | ||
}); | ||
test.doesNotThrow(function() { | ||
@@ -251,153 +267,124 @@ mock.bar(10, 20); | ||
exports.testMultipleEntriesForOneMethod = function(test) { | ||
var mock = nm.mock("foo").takes(10, 20).returns(30); | ||
mock.mock("foo").takes(10, 30).returns(40); | ||
test.doesNotThrow(function() { | ||
test.ok(mock.foo(10, 20) == 30); | ||
test.ok(mock.foo(10, 30) == 40); | ||
}); | ||
test.throws(function() { | ||
mock.foo(10); | ||
exports['assert returns'] = { | ||
setUp: function(cb) { | ||
this.error = console.error; | ||
console.error = function() {}; | ||
cb(); | ||
} | ||
, tearDown: function(cb) { | ||
console.error = this.error; | ||
cb(); | ||
} | ||
, 'false for uncalled multiple invocations of same method': function(test) { | ||
var mock = nm.mock('foo').takes(10, 20).returns(30) | ||
; | ||
mock.mock('foo').takes(10, 30).returns(40); | ||
mock.foo(10, 20); | ||
mock.bar(10, 30); | ||
}); | ||
test.done(); | ||
}; | ||
test.ok(!mock.assert()); | ||
test.done(); | ||
} | ||
exports.testAssertFailsSameMethod = function(test) { | ||
var mock = nm.named('mocky').mock("foo").takes(10, 20).returns(30); | ||
mock.mock("foo").takes(10, 30).returns(40); | ||
mock.foo(10,20); | ||
test.ok(!mock.assert()); | ||
test.done(); | ||
}; | ||
, 'false for uncalled invocation of a method': function(test) { | ||
var mock = nm.mock('foo').takes(10, 20).returns(30) | ||
; | ||
mock.mock('bar').takes(10, 30).returns(40); | ||
mock.foo(10, 20); | ||
test.ok(!mock.assert()); | ||
test.done(); | ||
} | ||
exports.testAssertFailsManyMethod = function(test) { | ||
var mock = nm.named('mockier').mock("foo").takes(10, 20).returns(30); | ||
mock.mock("bar").takes(10, 30).returns(40); | ||
mock.foo(10,20); | ||
test.ok(!mock.assert()); | ||
test.done(); | ||
}; | ||
, 'true for meeting all expectations': function(test) { | ||
var mock = nm.mock("foo").takes(10, 20).returns(30); | ||
mock.mock("foo").takes(10, 30).returns(40); | ||
mock.mock("bar").returns(30); | ||
mock.foo(10,20); | ||
mock.foo(10,30); | ||
mock.bar(); | ||
test.ok(mock.assert()); | ||
test.done(); | ||
} | ||
exports.testAssertOK = function(test) { | ||
var mock = nm.mock("foo").takes(10, 20).returns(30); | ||
mock.mock("foo").takes(10, 30).returns(40); | ||
mock.mock("bar").returns(30); | ||
mock.foo(10,20); | ||
mock.foo(10,30); | ||
mock.bar(); | ||
test.ok(mock.assert()); | ||
test.done(); | ||
}; | ||
, 'false for not meeting explicit times expectation': function(test) { | ||
var mock = nm.mock("foo").takes(10, 20).times(2); | ||
mock.foo(10, 20); | ||
mock.foo(10, 20); | ||
test.ok(mock.assert()); | ||
exports.testAssertThrowsFailed = function(test) { | ||
var mock = nm.mock("foo").takes(10, 20).returns(30); | ||
mock.mock("bar").takes(10, 30).returns(40); | ||
mock.foo(10,20); | ||
test.throws(function() { | ||
mock.assertThrows(); | ||
}); | ||
test.done(); | ||
}; | ||
mock = nm.mock("bar").takes(10).times(2); | ||
mock.bar(10); | ||
test.ok(!mock.assert()); | ||
exports.testAssertThrowsOK = function(test) { | ||
var mock = nm.mock("foo").takes(10, 20).returns(30); | ||
mock.mock("foo").takes(10, 30).returns(40); | ||
mock.mock("bar").returns(30); | ||
mock.foo(10,20); | ||
mock.foo(10,30); | ||
mock.bar(); | ||
test.doesNotThrow(function() { | ||
mock.assertThrows(); | ||
}); | ||
test.done(); | ||
test.done(); | ||
} | ||
}; | ||
exports.testTimes = function(test) { | ||
var mock = nm.mock("foo").takes(10, 20).times(2); | ||
mock.foo(10, 20); | ||
mock.foo(10, 20); | ||
test.ok(mock.assert()); | ||
exports['assertThrows'] = { | ||
'throws an error for missed expectation': function(test) { | ||
var mock = nm.mock("foo").takes(10, 20).returns(30); | ||
mock.mock("bar").takes(10, 30).returns(40); | ||
mock.foo(10,20); | ||
test.throws(function() { | ||
mock.assertThrows(); | ||
}); | ||
test.done(); | ||
} | ||
mock = nm.mock("bar").takes(10).times(2); | ||
mock.bar(10); | ||
test.ok(!mock.assert()); | ||
test.done(); | ||
}; | ||
, 'does not throw an error when all expectations met': function(test) { | ||
var mock = nm.mock("foo").takes(10, 20).returns(30); | ||
mock.mock("foo").takes(10, 30).returns(40); | ||
mock.mock("bar").returns(30); | ||
mock.foo(10,20); | ||
mock.foo(10,30); | ||
mock.bar(); | ||
test.doesNotThrow(function() { | ||
mock.assertThrows(); | ||
}); | ||
test.done(); | ||
} | ||
} | ||
exports.testCtrl = function(test) { | ||
exports['ctrl triggers the invocation of a callback'] = function(test) { | ||
test.expect(2); | ||
var ctrl = {}; | ||
var mock = nm.mock("foo").takes(10, 20, function() {}).ctrl(2, ctrl); | ||
mock.foo(10, 20, function(bb) { | ||
test.ok(bb = 20); | ||
}); | ||
ctrl.trigger(20); | ||
ctrl.trigger(20); | ||
test.done(); | ||
}; | ||
exports.testPartialCompareBug1 = function (test){ | ||
test.throws(function (){ | ||
var mock = nm.mock('foo').takes({hi: 1, bye: 2000},2,3) | ||
mock.foo({},2,3) //should throw because bye: 2000 is not present | ||
}) | ||
test.done() | ||
} | ||
exports['compare of compled arguments'] = { | ||
'fails for missing attributes': function(test) { | ||
test.throws(function (){ | ||
var mock = nm.mock('foo').takes({hi: 1, bye: 2000}, 2, 3); | ||
mock.foo({}, 2, 3); | ||
}); | ||
test.done(); | ||
} | ||
exports.testPartialCompareBug2 = function (test){ | ||
test.throws(function (){ | ||
var mock = nm.mock('foo').takes({hi: 1, bye: 2000},2,3) | ||
mock.foo({hi: 1},2,3) //should throw because bye: 2000 is not present | ||
mock.foo({},2,3) //should throw because bye: 2000 is not present | ||
}) | ||
test.done() | ||
, 'fails for missing attribute': function(test) { | ||
test.throws(function (){ | ||
var mock = nm.mock('foo').takes({hi: 1, bye: 2000}, 2, 3); | ||
mock.foo({hi: 1}, 2, 3); | ||
}); | ||
test.done(); | ||
} | ||
, 'fails for extra attributes': function(test) { | ||
test.doesNotThrow(function (){ | ||
var mock = nm.mock('foo').takes({hi: 1}, 2, 3); | ||
mock.foo({hi: 1, bye: 2000}, 2, 3); | ||
}); | ||
test.done(); | ||
} | ||
} | ||
exports.testPartialCompare = function (test){ | ||
test.doesNotThrow(function (){ | ||
var mock = nm.mock('foo').takes({hi: 1},2,3) | ||
mock.foo({hi: 1, bye: 2000},2,3) //should throw because bye: 2000 is not present | ||
}) | ||
test.throws(function (){ | ||
var mock = nm.mock('foo').takes({hi: 1},2,3) | ||
mock.foo({},2,3) //should throw because bye: 2000 is not present | ||
}) | ||
test.done() | ||
}; | ||
exports.multiReturn = function(test) { | ||
exports['mocked functions will return multiple values for multiple takens'] = function(test) { | ||
var mock = nm.mock("foo").returns(1); | ||
mock.mock("foo").returns(2); | ||
mock.mock("foo").takes(1).returns(42); | ||
test.equal(mock.foo(), 1, "good first return"); | ||
@@ -407,12 +394,9 @@ test.equal(mock.foo(), 2, "good second return"); | ||
test.ok(mock.assert(), 'bad invoked mocks'); | ||
test.throws(function() { | ||
test.equal(mock.foo(), 2, "re-uses second return"); | ||
}); | ||
test.done(); | ||
}; | ||
exports.testCopyFunction = function(test) { | ||
exports['mocked functions invokable without object context'] = function(test) { | ||
var mock = nm.mock('foo').returns(10); | ||
@@ -424,50 +408,35 @@ var foo = mock.foo; | ||
exports.testReset = function(test) { | ||
exports['reset mocks clears previous invocation count'] = function(test) { | ||
var mock = nm.mock('foo').returns(100); | ||
test.equal(mock.foo(), 100); | ||
test.ok(mock.assert()); | ||
mock.reset(); | ||
test.ok(!mock.foo); | ||
mock.mock('doo').returns(300); | ||
test.equal(mock.doo(), 300); | ||
test.ok(mock.assert()); | ||
test.done(); | ||
test.done(); | ||
}; | ||
exports.testIgnore = function(test) { | ||
exports['ignored mock functions do not throw on multiple invocations'] = function(test) { | ||
var mock = nm.mock('foo').returns(100); | ||
test.equal(mock.foo(), 100); | ||
mock.ignore('hello'); | ||
test.ok(mock.hello); | ||
test.doesNotThrow(function() { | ||
mock.hello(); | ||
}); | ||
test.done(); | ||
}; | ||
exports.testIgnoreRootMethod = function(test) { | ||
exports['ignored mock function from library does not throw on multiple invocations'] = function(test) { | ||
var mock = nm.ignore('hello'); | ||
test.ok(mock.hello); | ||
test.doesNotThrow(function() { | ||
mock.hello(); | ||
}); | ||
test.done(); | ||
}; | ||
exports.testIgnoreAfterReset = function(test) { | ||
exports['ignored honored after resetting mock'] = function(test) { | ||
var mock = nm.mock('foo').returns(10); | ||
@@ -477,20 +446,15 @@ mock.reset(); | ||
test.ok(mock.hello); | ||
test.doesNotThrow(function() { | ||
mock.hello(); | ||
}); | ||
test.done(); | ||
}; | ||
exports.testFailThrowsNoExceptionWhenNotCalled = function(test){ | ||
exports['failed methods not tracked for assertions'] = function(test) { | ||
var mock = nm.mock('test').fail(); | ||
test.doesNotThrow(function() { | ||
// the method was not called so, no exception should be thrown | ||
mock.assertThrows(); | ||
}); | ||
test.done(); | ||
} | ||
}; | ||
@@ -512,5 +476,5 @@ exports.testTakesF = function(test) { | ||
var mock = nm.mock("withFunction").takesF(takesValidator).returns(4); | ||
test.equals(mock.withFunction(3), 4, "takesF test correct"); | ||
test.equals(mock.withFunction(3), 4, "takesF test correct"); | ||
}) | ||
test.done(); | ||
@@ -523,3 +487,3 @@ } | ||
test.equals(mock.withAll("whatever"), 4, "takesAll test correct"); | ||
test.done(); | ||
test.done(); | ||
} | ||
@@ -542,2 +506,14 @@ | ||
test.done(); | ||
} | ||
} | ||
exports['compares buffers ok'] = function (test) { | ||
var mock = nm.mock('foo').takes(new Buffer('123')).returns(1); | ||
test.equals(mock.foo(new Buffer('123')), 1); | ||
mock = nm.mock('foo').takes(new Buffer('123')).returns(1); | ||
test.throws(function() { | ||
test.equals(mock.foo(new Buffer('234')), 1); | ||
}); | ||
test.done(); | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
35034
791