From afca0689f780a76f515e4674ae3cf95924cb0fb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Pa=CC=88rsson?= Date: Tue, 3 Feb 2015 15:24:11 +0100 Subject: [PATCH 1/2] Add support for chaining and calls on Jasmine spies --- jasmine/jasmine-tests.ts | 38 ++++++++++++++++++++++++++++++++++++++ jasmine/jasmine.d.ts | 8 ++++---- 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/jasmine/jasmine-tests.ts b/jasmine/jasmine-tests.ts index 8a4a00e22b..1a9b1b2e46 100644 --- a/jasmine/jasmine-tests.ts +++ b/jasmine/jasmine-tests.ts @@ -400,6 +400,44 @@ describe("A spy, when configured to throw a value", function () { }); }); +describe("A spy, when configured with multiple actions", function () { + var foo: any, bar: any, fetchedBar: any; + + beforeEach(function () { + foo = { + setBar: function (value: any) { + bar = value; + }, + getBar: function () { + return bar; + } + }; + + spyOn(foo, 'getBar').and.callThrough().and.callFake(() => { + this.fakeCalled = true; + }); + + foo.setBar(123); + fetchedBar = foo.getBar(); + }); + + it("tracks that the spy was called", function () { + expect(foo.getBar).toHaveBeenCalled(); + }); + + it("should not effect other functions", function () { + expect(bar).toEqual(123); + }); + + it("when called returns the requested value", function () { + expect(fetchedBar).toEqual(123); + }); + + it("should have called the fake implementation", function () { + expect(this.fakeCalled).toEqual(true); + }); +}); + describe("A spy", function () { var foo: any, bar: any = null; diff --git a/jasmine/jasmine.d.ts b/jasmine/jasmine.d.ts index e94e5fce1f..6c323e1466 100644 --- a/jasmine/jasmine.d.ts +++ b/jasmine/jasmine.d.ts @@ -1,6 +1,6 @@ // Type definitions for Jasmine 2.1 // Project: http://pivotal.github.com/jasmine/ -// Definitions by: Boris Yankov , Theodore Brown +// Definitions by: Boris Yankov , Theodore Brown , David Pärsson // Definitions: https://github.com/borisyankov/DefinitelyTyped @@ -372,15 +372,15 @@ declare module jasmine { interface SpyAnd { /** By chaining the spy with and.callThrough, the spy will still track all calls to it but in addition it will delegate to the actual implementation. */ - callThrough(): void; + callThrough(): Spy; /** By chaining the spy with and.returnValue, all calls to the function will return a specific value. */ returnValue(val: any): void; /** By chaining the spy with and.callFake, all calls to the spy will delegate to the supplied function. */ - callFake(fn: Function): void; + callFake(fn: Function): Spy; /** By chaining the spy with and.throwError, all calls to the spy will throw the specified value. */ throwError(msg: string): void; /** When a calling strategy is used for a spy, the original stubbing behavior can be returned at any time with and.stub. */ - stub(): void; + stub(): Spy; } interface Calls { From 675a63e4036624d24b06f1b173a92cbe7bb72ab7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Pa=CC=88rsson?= Date: Wed, 4 Feb 2015 11:28:29 +0100 Subject: [PATCH 2/2] Added global fail() function to Jasmine typings --- jasmine/jasmine-tests.ts | 16 ++++++++++++++++ jasmine/jasmine.d.ts | 2 ++ 2 files changed, 18 insertions(+) diff --git a/jasmine/jasmine-tests.ts b/jasmine/jasmine-tests.ts index 1a9b1b2e46..cbac45d169 100644 --- a/jasmine/jasmine-tests.ts +++ b/jasmine/jasmine-tests.ts @@ -714,6 +714,22 @@ describe("Asynchronous specs", function () { }); }); +describe("Fail", function () { + + it("should fail test when called without arguments", function () { + fail(); + }); + + it("should fail test when called with a fail message", function () { + fail("The test failed"); + }); + + it("should fail test when called an error", function () { + fail(new Error("The test failed with this error")); + }); + +}); + (() => { // from boot.js var env = jasmine.getEnv(); diff --git a/jasmine/jasmine.d.ts b/jasmine/jasmine.d.ts index 6c323e1466..3d5e48669a 100644 --- a/jasmine/jasmine.d.ts +++ b/jasmine/jasmine.d.ts @@ -33,6 +33,8 @@ declare function afterAll(action: (done: () => void) => void): void; declare function expect(spy: Function): jasmine.Matchers; declare function expect(actual: any): jasmine.Matchers; +declare function fail(e?: any): void; + declare function spyOn(object: any, method: string): jasmine.Spy; declare function runs(asyncMethod: Function): void;