Skip to content

Commit

Permalink
Separated spy 'stand-ins' from SpyDelegates
Browse files Browse the repository at this point in the history
  • Loading branch information
Davis W. Frank and Sheel Choksi committed Jul 2, 2013
1 parent 9805f70 commit be9df7e
Show file tree
Hide file tree
Showing 15 changed files with 243 additions and 169 deletions.
35 changes: 35 additions & 0 deletions spec/core/EnvSpec.js
Expand Up @@ -51,6 +51,41 @@ describe("Env", function() {
});
});

describe("#isSpy", function() {
it("reports whether the object is a spy", function() {
var spy = env.createSpy('genericSpy');

expect(env.isSpy(spy)).toBe(true);
});

it("handles being passed in undefined as a spy", function() {
expect(env.isSpy(void 0)).toBe(false);
});
});

describe("#createSpy", function() {
it("returns a function that corresponds to a spy", function() {
var spy = env.createSpy({name: "foo"});

expect(spy instanceof Function).toBe(true);
});

it("copies any properties on an original function to the stand-in", function() {
var originalFn = function() {},
spy;

originalFn.bar = "baz";
spy = env.createSpy({name: "foo", originalFn: originalFn});

expect(spy.bar).toEqual("baz");
});

it("registers a SpyDelegate for tracking calls", function() {
var spy = env.createSpy({});

expect(env.spyRegistry.lookup(spy) instanceof j$.SpyDelegate).toBe(true);
});
});
});

// TODO: move these into a separate file
Expand Down
10 changes: 2 additions & 8 deletions spec/core/PrettyPrintSpec.js
Expand Up @@ -103,14 +103,8 @@ describe("j$.pp", function () {
});

it("should stringify spy objects properly", function() {
var TestObject = {
someFunction: function() {
}
};
spyOn(TestObject, 'someFunction');
expect(j$.pp(TestObject.someFunction)).toEqual("spy on someFunction");

expect(j$.pp(jasmine.createSpy("something"))).toEqual("spy on something");
var spy = j$.createSpy('someFunction');
expect(j$.pp(spy)).toEqual("spy on someFunction");
});

it("should stringify objects that implement jasmineToString", function () {
Expand Down
28 changes: 28 additions & 0 deletions spec/core/SpyRegistrySpec.js
@@ -0,0 +1,28 @@
describe("SpyRegistry", function() {
it("allows registering a spy to a spyDelegate", function() {
var spyRegistry = new j$.SpyRegistry(),
standIn = function() {},
delegate = {};

spyRegistry.register(standIn, delegate);
expect(spyRegistry.lookup(standIn)).toEqual(delegate);
});

it("is undefined when the lookup key does not exist", function() {
var spyRegistry = new j$.SpyRegistry();

expect(spyRegistry.lookup(function() {})).toBeUndefined();
});

it("resetting the registry", function() {
var spyRegistry = new j$.SpyRegistry(),
standIn = function() {},
delegate = {};

spyRegistry.register(standIn, delegate);

spyRegistry.reset();

expect(spyRegistry.lookup(standIn)).toBeUndefined();
});
});
90 changes: 14 additions & 76 deletions spec/core/SpySpec.js
Expand Up @@ -34,7 +34,7 @@ describe("SpyDelegate", function() {

it("tracks the params from each execution", function() {
var spyDelegate = new j$.SpyDelegate(),
args;
args;

spyDelegate.exec();
spyDelegate.exec(0, "foo");
Expand All @@ -54,7 +54,7 @@ describe("SpyDelegate", function() {

it("tracks the context and arguments for each call", function() {
var spyDelegate = new j$.SpyDelegate(),
args;
args;

spyDelegate.exec();
spyDelegate.exec(0, "foo");
Expand Down Expand Up @@ -100,7 +100,7 @@ describe("SpyDelegate", function() {

it("stubs an original function, if provided", function() {
var originalFn = jasmine.createSpy("original"),
spyDelegate = new j$.SpyDelegate({fn: originalFn});
spyDelegate = new j$.SpyDelegate({fn: originalFn});

spyDelegate.exec();

Expand All @@ -109,8 +109,8 @@ describe("SpyDelegate", function() {

it("allows an original function to be called, passed through the params and returns it's value", function() {
var originalFn = jasmine.createSpy("original").andReturn(42),
spyDelegate = new j$.SpyDelegate({fn: originalFn}),
returnValue;
spyDelegate = new j$.SpyDelegate({fn: originalFn}),
returnValue;

spyDelegate.callThrough();
returnValue = spyDelegate.exec("foo");
Expand All @@ -122,8 +122,8 @@ describe("SpyDelegate", function() {

it("can return a specified value when executed", function() {
var originalFn = jasmine.createSpy("original"),
spyDelegate = new j$.SpyDelegate({fn: originalFn}),
returnValue;
spyDelegate = new j$.SpyDelegate({fn: originalFn}),
returnValue;

spyDelegate.return(17);
returnValue = spyDelegate.exec();
Expand All @@ -134,19 +134,21 @@ describe("SpyDelegate", function() {

it("allows an exception to be thrown when executed", function() {
var originalFn = jasmine.createSpy("original"),
spyDelegate = new j$.SpyDelegate({fn: originalFn});
spyDelegate = new j$.SpyDelegate({fn: originalFn});

spyDelegate.throw("bar");

expect(function() { spyDelegate.exec(); }).toThrow("bar");
expect(function() {
spyDelegate.exec();
}).toThrow("bar");
expect(originalFn).not.toHaveBeenCalled();
});

it("allows a fake function to be called instead", function() {
var originalFn = jasmine.createSpy("original"),
fakeFn = jasmine.createSpy("fake").andReturn(67),
spyDelegate = new j$.SpyDelegate({fn: originalFn}),
returnValue;
fakeFn = jasmine.createSpy("fake").andReturn(67),
spyDelegate = new j$.SpyDelegate({fn: originalFn}),
returnValue;

spyDelegate.callFake(fakeFn);
returnValue = spyDelegate.exec();
Expand All @@ -156,70 +158,6 @@ describe("SpyDelegate", function() {
});
});

describe("createSpy", function() {

it("returns a function that has a SpyDelegate", function() {
var spy = j$.createSpy();

expect(spy instanceof Function).toBe(true);
expect(spy.and instanceof j$.SpyDelegate).toBe(true);
expect(spy.get).toEqual(spy.and);
});

it("says that it is a spy", function() {
var spy = j$.createSpy();

expect(spy.isSpy).toBe(true);
});

it("keeps its identity", function() {
var spy = j$.createSpy("foo");

expect(spy.get.identity()).toEqual("foo");
});

it("acts like a spy for call tracking", function() {
var spy = j$.createSpy();

spy("foo");

expect(spy.get.callCount()).toEqual(1);
expect(spy.get.mostRecentCall()).toEqual({object: window, args: ["foo"]});
});

it("acts like a spy for configuration", function() {
var originalFn = jasmine.createSpy("original").andReturn(17),
spy = j$.createSpy("foo", originalFn),
returnValue;

spy();

expect(originalFn).not.toHaveBeenCalled();

originalFn.reset();
spy.and.reset();

spy.and.callThrough();
returnValue = spy();

expect(originalFn).toHaveBeenCalled();
expect(returnValue).toEqual(17);

originalFn.reset();
spy.and.reset();

spy.and.return(42);
returnValue = spy();

expect(originalFn).not.toHaveBeenCalled();
expect(returnValue).toEqual(42);
});
});

describe("isSpy", function() {
// TODO: fill this in
});

describe("createSpyObj", function() {
// TODO: fill this in
});
3 changes: 2 additions & 1 deletion spec/core/matchers/toBeNaNSpec.js
Expand Up @@ -9,7 +9,8 @@ describe("toBeNaN", function() {
});

it("fails for anything not a NaN", function() {
var matcher = j$.matchers.toBeNaN();
var matcher = j$.matchers.toBeNaN(),
result;

result = matcher.compare(1);
expect(result.pass).toBe(false);
Expand Down
45 changes: 33 additions & 12 deletions spec/core/matchers/toHaveBeenCalledSpec.js
@@ -1,8 +1,13 @@
describe("toHaveBeenCalled", function() {
it("passes when the actual was called, with a custom .not fail message", function() {
var matcher = j$.matchers.toHaveBeenCalled(),
calledSpy = jasmine.createSpy('called-spy'),
result;
var fakeSpyDelegate = {
wasCalled: function() { return true; },
identity: function() { return "called-spy"; }
},
util = { spyLookup: function() { return fakeSpyDelegate; } },
matcher = j$.matchers.toHaveBeenCalled(util),
calledSpy = jasmine.createSpy("called-spy"),
result;

calledSpy();

Expand All @@ -12,31 +17,47 @@ describe("toHaveBeenCalled", function() {
});

it("fails when the actual was not called", function() {
var matcher = j$.matchers.toHaveBeenCalled(),
uncalledSpy = jasmine.createSpy('uncalled spy');
var fakeSpyDelegate = {
wasCalled: function() { return false; },
identity: function() { return "uncalled-spy"; }
},
util = { spyLookup: function() { return fakeSpyDelegate; } },
matcher = j$.matchers.toHaveBeenCalled(util),
uncalledSpy = jasmine.createSpy("uncalled-spy"),
result;

result = matcher.compare(uncalledSpy);
expect(result.pass).toBe(false);
});

it("throws an exception when the actual is not a spy", function() {
var matcher = j$.matchers.toHaveBeenCalled(),
fn = function() {};
var util = { spyLookup: function() { return void 0; } },
matcher = j$.matchers.toHaveBeenCalled(util),
fn = function() {};

expect(function() { matcher.compare(fn) }).toThrow(new Error("Expected a spy, but got Function."));
});

it("throws an exception when invoked with any arguments", function() {
var matcher = j$.matchers.toHaveBeenCalled(),
spy = jasmine.createSpy('sample spy');
var fakeSpyDelegate = {
wasCalled: function() { return true; }
},
util = { spyLookup: function() { return fakeSpyDelegate; } },
matcher = j$.matchers.toHaveBeenCalled(util),
spy = jasmine.createSpy('sample spy');

expect(function() { matcher.compare(spy, 'foo') }).toThrow(new Error("toHaveBeenCalled does not take arguments, use toHaveBeenCalledWith"));
});

it("has a custom message on failure", function() {
var matcher = j$.matchers.toHaveBeenCalled(),
spy = jasmine.createSpy('sample-spy'),
result;
var fakeSpyDelegate = {
wasCalled: function() { return false; },
identity: function() { return "sample-spy"; }
},
util = { spyLookup: function() { return fakeSpyDelegate; } },
matcher = j$.matchers.toHaveBeenCalled(util),
spy = jasmine.createSpy("sample-spy"),
result;

result = matcher.compare(spy);

Expand Down

0 comments on commit be9df7e

Please sign in to comment.