public/qunit/qunit.mock.js in entityjs-0.3.1 vs public/qunit/qunit.mock.js in entityjs-0.3.2

- old
+ new

@@ -1,30 +1,71 @@ +//TODO: fix up +/* + +Implements: + +//expects the method to be called before the end of the test +expectCall(re, 'pressed') + +//expects a method to be called with the given arguments +expectCall(re, 'pressed', ['a']) + +//expects a method to be called a number of times +expectCall(re, 'pressed', null, 2) + +//stubs a method with a custom method +stub(re, 'pressed', function(value){ + return false; +}); + +//method does nothing +stub(re, 'pressed'); + +//method returns 10 +stub(re, 'pressed', 10); + +*/ (function() { var expectCall, finishMock, mock, mocked, mocking, stack, stub, testExpectations; var __slice = Array.prototype.slice; mocking = null; stack = []; - expectCall = function(object, method, calls) { + expectCall = function(object, method, args, calls) { var expectation; calls = (typeof calls !== "undefined" && calls !== null) ? calls : 1; + expectation = { object: object, method: method, expectedCalls: calls, + expectedArgs: args, originalMethod: object[method], callCount: 0 }; object[method] = function() { var args; args = __slice.call(arguments, 0); expectation.originalMethod.apply(object, args); + + expectation.args = args; + return expectation.callCount += 1; }; return mocking.expectations.push(expectation); }; stub = function(object, method, fn) { var stb; + + if(fn == null){ + fn = function(){}; + } else if(typeof fn != 'function'){ + var value = fn; + fn = function(){ + return value; + }; + } + stb = { object: object, method: method, original: object[method] }; @@ -54,10 +95,21 @@ }; testExpectations = function() { var _a, expectation, stb; while (mocking.expectations.length > 0) { expectation = mocking.expectations.pop(); - equals(expectation.callCount, expectation.expectedCalls, "method " + (expectation.method) + " should be called " + (expectation.expectedCalls) + " times"); + equal(expectation.callCount, expectation.expectedCalls, "method " + (expectation.method) + " should be called " + (expectation.expectedCalls) + " times"); + + if(expectation.expectedArgs){ + //equal(expectation.expectedArgs.length, expectation.args.length, "method " + (expectation.method) + " should have " + (expectation.expectedArgs.length) + " arguments") + + //check args + var eargs = expectation.expectedArgs; + var args = expectation.args; + ok(!(eargs>args || eargs<args), "method "+ expectation.method +" should have same arguments"); + + } + expectation.object[expectation.method] = expectation.originalMethod; } _a = []; while (mocking.stubs.length > 0) { _a.push((function() { \ No newline at end of file