2 var Gently = require('gently')
6 process.removeAllListeners('exit');
11 test(function constructor() {
12 assert.deepEqual(gently.expectations, []);
13 assert.deepEqual(gently.hijacked, {});
14 assert.equal(gently.constructor.name, 'Gently');
17 test(function expectBadArgs() {
18 var BAD_ARG = 'oh no';
20 gently.expect(BAD_ARG);
21 assert.ok(false, 'throw needs to happen');
23 assert.equal(e.message, 'Bad 1st argument for gently.expect(), object, function, or number expected, got: '+(typeof BAD_ARG));
27 test(function expectObjMethod() {
28 var OBJ = {}, NAME = 'foobar';
29 OBJ.foo = function(x) {
33 gently._name = function() {
37 var original = OBJ.foo
38 , stubFn = function() {};
40 (function testAddOne() {
41 assert.strictEqual(gently.expect(OBJ, 'foo', stubFn), original);
43 assert.equal(gently.expectations.length, 1);
44 var expectation = gently.expectations[0];
45 assert.strictEqual(expectation.obj, OBJ);
46 assert.strictEqual(expectation.method, 'foo');
47 assert.strictEqual(expectation.stubFn, stubFn);
48 assert.strictEqual(expectation.name, NAME);
49 assert.strictEqual(OBJ.foo._original, original);
52 (function testAddTwo() {
53 gently.expect(OBJ, 'foo', 2, stubFn);
54 assert.equal(gently.expectations.length, 2);
55 assert.strictEqual(OBJ.foo._original, original);
58 (function testAddOneWithoutMock() {
59 gently.expect(OBJ, 'foo');
60 assert.equal(gently.expectations.length, 3);
63 var stubFnCalled = 0, SELF = {};
64 gently._stubFn = function(self, obj, method, name, args) {
66 assert.strictEqual(self, SELF);
67 assert.strictEqual(obj, OBJ);
68 assert.strictEqual(method, 'foo');
69 assert.strictEqual(name, NAME);
70 assert.deepEqual(args, [1, 2]);
73 assert.equal(OBJ.foo.apply(SELF, [1, 2]), 23);
74 assert.equal(stubFnCalled, 1);
77 test(function expectClosure() {
78 var NAME = 'MY CLOSURE';
79 function closureFn() {};
81 gently._name = function() {
85 var fn = gently.expect(closureFn);
86 assert.equal(gently.expectations.length, 1);
87 var expectation = gently.expectations[0];
88 assert.strictEqual(expectation.obj, null);
89 assert.strictEqual(expectation.method, null);
90 assert.strictEqual(expectation.stubFn, closureFn);
91 assert.strictEqual(expectation.name, NAME);
93 var stubFnCalled = 0, SELF = {};
94 gently._stubFn = function(self, obj, method, name, args) {
96 assert.strictEqual(self, SELF);
97 assert.strictEqual(obj, null);
98 assert.strictEqual(method, null);
99 assert.strictEqual(name, NAME);
100 assert.deepEqual(args, [1, 2]);
103 assert.equal(fn.apply(SELF, [1, 2]), 23);
104 assert.equal(stubFnCalled, 1);
107 test(function expectClosureCount() {
108 var stubFnCalled = 0;
109 function closureFn() {stubFnCalled++};
111 var fn = gently.expect(2, closureFn);
112 assert.equal(gently.expectations.length, 1);
114 assert.equal(gently.expectations.length, 1);
116 assert.equal(stubFnCalled, 2);
119 test(function restore() {
120 var OBJ = {}, NAME = '[my object].myFn()';
121 OBJ.foo = function(x) {
125 gently._name = function() {
129 var original = OBJ.foo;
130 gently.expect(OBJ, 'foo');
131 gently.restore(OBJ, 'foo');
132 assert.strictEqual(OBJ.foo, original);
134 (function testError() {
136 gently.restore(OBJ, 'foo');
137 assert.ok(false, 'throw needs to happen');
139 assert.equal(e.message, NAME+' is not gently stubbed');
144 test(function _stubFn() {
145 var OBJ1 = {toString: function() {return '[OBJ 1]'}}
146 , OBJ2 = {toString: function() {return '[OBJ 2]'}, foo: function () {return 'bar';}}
149 gently.expect(OBJ1, 'foo', function(x) {
150 assert.strictEqual(this, SELF);
154 assert.equal(gently._stubFn(SELF, OBJ1, 'foo', 'dummy_name', [5]), 10);
156 (function testAutorestore() {
157 assert.equal(OBJ2.foo(), 'bar');
159 gently.expect(OBJ2, 'foo', function() {
160 return 'stubbed foo';
163 gently.expect(OBJ2, 'foo', function() {
164 return "didn't restore yet";
167 assert.equal(gently._stubFn(SELF, OBJ2, 'foo', 'dummy_name', []), 'stubbed foo');
168 assert.equal(gently._stubFn(SELF, OBJ2, 'foo', 'dummy_name', []), "didn't restore yet");
169 assert.equal(OBJ2.foo(), 'bar');
170 assert.deepEqual(gently.expectations, []);
173 (function testNoMoreCallExpected() {
175 gently._stubFn(SELF, OBJ1, 'foo', 'dummy_name', [5]);
176 assert.ok(false, 'throw needs to happen');
178 assert.equal(e.message, 'Unexpected call to dummy_name, no call was expected');
182 (function testDifferentCallExpected() {
183 gently.expect(OBJ2, 'bar');
185 gently._stubFn(SELF, OBJ1, 'foo', 'dummy_name', [5]);
186 assert.ok(false, 'throw needs to happen');
188 assert.equal(e.message, 'Unexpected call to dummy_name, expected call to '+gently._name(OBJ2, 'bar'));
191 assert.equal(gently.expectations.length, 1);
194 (function testNoMockCallback() {
196 assert.equal(gently.expectations.length, 0);
200 test(function stub() {
201 var LOCATION = './my_class';
203 (function testRegular() {
204 var Stub = gently.stub(LOCATION);
205 assert.ok(Stub instanceof Function);
206 assert.strictEqual(gently.hijacked[LOCATION], Stub);
207 assert.ok(Stub['new'] instanceof Function);
208 assert.equal(Stub.toString(), 'require('+JSON.stringify(LOCATION)+')');
210 (function testConstructor() {
213 , ARGS = ['foo', 'bar'];
215 Stub['new'] = function(a, b) {
216 assert.equal(a, ARGS[0]);
217 assert.equal(b, ARGS[1]);
222 var stub = new Stub(ARGS[0], ARGS[1]);
223 assert.strictEqual(stub, STUB);
224 assert.equal(newCalled, 1);
225 assert.equal(stub.toString(), 'require('+JSON.stringify(LOCATION)+')');
228 (function testUseReturnValueAsInstance() {
231 Stub['new'] = function() {
235 var stub = new Stub();
236 assert.strictEqual(stub, R);
241 var EXPORTS_NAME = 'MyClass';
242 test(function testExportsName() {
243 var Stub = gently.stub(LOCATION, EXPORTS_NAME);
244 assert.strictEqual(gently.hijacked[LOCATION][EXPORTS_NAME], Stub);
245 assert.equal(Stub.toString(), 'require('+JSON.stringify(LOCATION)+').'+EXPORTS_NAME);
247 (function testConstructor() {
248 var stub = new Stub();
249 assert.equal(Stub.toString(), 'require('+JSON.stringify(LOCATION)+').'+EXPORTS_NAME);
254 test(function hijack() {
255 var LOCATION = './foo'
258 , REQUIRE = function() {
263 var hijackedRequire = gently.hijack(REQUIRE);
264 hijackedRequire(LOCATION);
265 assert.strictEqual(gently.hijacked[LOCATION], EXPORTS);
267 assert.equal(REQUIRE_CALLS, 1);
269 // make sure we are caching the hijacked module
270 hijackedRequire(LOCATION);
271 assert.equal(REQUIRE_CALLS, 1);
274 test(function verify() {
275 var OBJ = {toString: function() {return '[OBJ]'}};
278 gently.expect(OBJ, 'foo');
281 assert.ok(false, 'throw needs to happen');
283 assert.equal(e.message, 'Expected call to [OBJ].foo() did not happen');
287 gently.verify('foo');
288 assert.ok(false, 'throw needs to happen');
290 assert.equal(e.message, 'Expected call to [OBJ].foo() did not happen (foo)');
294 test(function processExit() {
295 var verifyCalled = 0;
296 gently.verify = function(msg) {
298 assert.equal(msg, 'process exit');
301 process.emit('exit');
302 assert.equal(verifyCalled, 1);
305 test(function _name() {
306 (function testNamedClass() {
309 assert.equal(gently._name(foo, 'bar'), '[Foo].bar()');
312 (function testToStringPreference() {
314 Foo.prototype.toString = function() {
315 return '[Superman 123]';
318 assert.equal(gently._name(foo, 'bar'), '[Superman 123].bar()');
321 (function testUnamedClass() {
322 var Foo = function() {};
324 assert.equal(gently._name(foo, 'bar'), foo.toString()+'.bar()');
327 (function testNamedClosure() {
328 function myClosure() {};
329 assert.equal(gently._name(null, null, myClosure), myClosure.name+'()');
332 (function testUnamedClosure() {
333 var myClosure = function() {2+2 == 5};
334 assert.equal(gently._name(null, null, myClosure), '>> '+myClosure.toString()+' <<');
338 test(function verifyExpectNone() {
339 var OBJ = {toString: function() {return '[OBJ]'}};
342 gently.expect(OBJ, 'foo', 0);
346 assert.fail('Exception should not have been thrown');