Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Strongly-typed wrappers for the "spy" portion of Sinon. See example usage in `src/examples/examples.ts` (which can be run in a browser by opening `dist/examples/examples.html`).
Strongly-typed wrappers for the "spy" and "stub" portion of Sinon. See example usage in `src/examples/examples.ts` (which can be run in a browser by opening `dist/examples/examples.html`), `src/examples/class-spy` and `src/examples/stubs`.

Totally unsupported. Please don't contact me about this!
1 change: 1 addition & 0 deletions dist/TypeSinon.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ declare module TypeSinon {
interface Spy<TFunc> extends SinonSpy {
fn: TFunc;
}
function stub<TFunc>(obj?: any, method?: TFunc, fn?: TFunc): SinonStub;
}
16 changes: 15 additions & 1 deletion dist/TypeSinon.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,20 @@ var TypeSinon;
return (obj[key] = spyOnFunc(method));
}
}
throw new Error("Method found on object.\nMethod: " + method + "\nObject: " + obj);
throw new Error("Method not found on object.\nMethod: " + method + "\nObject: " + obj);
}
function stub(obj, method, fn) {
if (obj && method) {
for (var key in obj) {
if (obj[key] === method) {
return sinon.stub(obj, key, fn);
}
}
throw new Error("Method not found on object.\nMethod: " + method + "\nObject: " + obj);
}
else {
return sinon.stub(obj);
}
}
TypeSinon.stub = stub;
})(TypeSinon || (TypeSinon = {}));
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions dist/examples/Staff.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var Staff;
myAnimals.forEach(function (animal) {
_this._zooService.feedAnimalByName(animal.name);
});
console.log("Employee duties performed.");
};
return Employee;
})();
Expand Down
2 changes: 1 addition & 1 deletion dist/examples/class-spy/tests.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/// <reference path="../../TypeSinon.d.ts" />
/// <reference path="App.d.ts" />
/// <reference path="../App.d.ts" />
declare var engine: App.Engine, engineStartSpy: TypeSinon.Spy<() => void>;
declare var engine: App.Engine, engineStopSpy: TypeSinon.Spy<() => void>;
2 changes: 1 addition & 1 deletion dist/examples/class-spy/tests.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<script src="http://sinonjs.org/releases/sinon-1.12.2.js"></script>
<script src="../../TypeSinon.js"></script>
<script src="App.js"></script>
<script src="../App.js"></script>
<script src="tests.js"></script>
</body>
</html>
2 changes: 1 addition & 1 deletion dist/examples/class-spy/tests.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/// <reference path="../../TypeSinon.ts" />
/// <reference path="App.ts" />
/// <reference path="../App.ts" />
//test 1: should change state from stopped to started on power button press
// Arrange
var engine = new App.Engine("stopped"), engineStartSpy = TypeSinon.spy(engine, engine.start);
Expand Down
11 changes: 11 additions & 0 deletions dist/examples/stubs/example-stubs.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/// <reference path="../../TypeSinon.d.ts" />
/// <reference path="../App.d.ts" />
declare var stub1: SinonStub;
declare var engine2: App.Engine;
declare var stub2: SinonStub;
declare var engine3: App.Engine;
declare var stub3: SinonStub;
declare var engine4: App.Engine;
declare var fakeStartFunc: () => void;
declare var stub4: SinonStub;
declare var wrongFakePerformDutiesFunc: (p: number) => boolean;
11 changes: 11 additions & 0 deletions dist/examples/stubs/example-stubs.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
See browser console for output.

<script src="http://sinonjs.org/releases/sinon-1.12.2.js"></script>
<script src="../../TypeSinon.js"></script>
<script src="../App.js"></script>
<script src="example-stubs.js"></script>
</body>
</html>
40 changes: 40 additions & 0 deletions dist/examples/stubs/example-stubs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/// <reference path="../../TypeSinon.ts" />
/// <reference path="../App.ts" />
// simple stub
var stub1 = TypeSinon.stub().throws();
try {
stub1();
}
catch (e) {
console.log("stub1 throws");
}
// stub existing object
var engine2 = new App.Engine();
console.info("engine.getState method before creating stub returns", engine2.getState());
var stub2 = TypeSinon.stub(engine2);
console.info("engine.getState method after creating stub returns", engine2.getState());
// stub method on existing object
var engine3 = new App.Engine();
var stub3 = TypeSinon.stub(engine3, engine3.start).throws();
try {
engine3.start();
}
catch (e) {
console.log("stub3 throws");
console.info("engine.getState method should still return state properly:", engine3.getState());
}
// stub method on existing object with given (as param) function
var engine4 = new App.Engine();
var fakeStartFunc = function () {
console.log("fake startEngine");
};
var stub4 = TypeSinon.stub(engine4, engine4.start, fakeStartFunc);
engine4.start();
var wrongFakePerformDutiesFunc = function (p) {
console.log("wrong fake performDuties");
return true;
};
// below will not work, TS compiler will return following error:
// Type argument candidate '() => void' is not a valid type argument because it is not a supertype of candidate '(p: number) => boolean'.
// TypeSinon.stub(engine4, engine4.start, wrongFakePerformDutiesFunc);
// engine4.start();
17 changes: 16 additions & 1 deletion src/TypeSinon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,25 @@ module TypeSinon {
}
}

throw new Error("Method found on object.\nMethod: " + method + "\nObject: " + obj);
throw new Error("Method not found on object.\nMethod: " + method + "\nObject: " + obj);
}

export interface Spy<TFunc> extends SinonSpy {
fn: TFunc;
}

export function stub<TFunc>(obj?: any, method?: TFunc, fn?: TFunc): SinonStub {
if (obj && method) {
// Find the function and stub it
for (var key in obj) {
if (obj[key] === method) {
return sinon.stub(obj, key, fn);
}
}

throw new Error("Method not found on object.\nMethod: " + method + "\nObject: " + obj);
} else {
return sinon.stub(obj);
}
}
}
3 changes: 1 addition & 2 deletions src/examples/class-spy/App.ts → src/examples/App.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

module App {
/**
* A class that can be tested.
Expand Down Expand Up @@ -32,4 +31,4 @@ module App {
return this._state;
}
}
}
}
1 change: 1 addition & 0 deletions src/examples/Staff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module Staff {
myAnimals.forEach((animal) => {
this._zooService.feedAnimalByName(animal.name);
});
console.log("Employee duties performed.");
}
}
}
2 changes: 1 addition & 1 deletion src/examples/class-spy/tests.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<script src="http://sinonjs.org/releases/sinon-1.12.2.js"></script>
<script src="../../TypeSinon.js"></script>
<script src="App.js"></script>
<script src="../App.js"></script>
<script src="tests.js"></script>
</body>
</html>
2 changes: 1 addition & 1 deletion src/examples/class-spy/tests.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/// <reference path="../../TypeSinon.ts" />
/// <reference path="App.ts" />
/// <reference path="../App.ts" />


//test 1: should change state from stopped to started on power button press
Expand Down
2 changes: 1 addition & 1 deletion src/examples/examples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ harold.performDuties();
console.log("Animals fed: " + mockFeedAnimals.args);

// ... and 'called' just gives a bool to say whether the func was invoked at all.
console.log("Will the zoo self-destruct? " + mockBeginSelfDestruct.called);
console.log("Will the zoo self-destruct? " + mockBeginSelfDestruct.called);
11 changes: 11 additions & 0 deletions src/examples/stubs/example-stubs.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
See browser console for output.

<script src="http://sinonjs.org/releases/sinon-1.12.2.js"></script>
<script src="../../TypeSinon.js"></script>
<script src="../App.js"></script>
<script src="example-stubs.js"></script>
</body>
</html>
50 changes: 50 additions & 0 deletions src/examples/stubs/example-stubs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/// <reference path="../../TypeSinon.ts" />
/// <reference path="../App.ts" />

// simple stub

var stub1 = TypeSinon.stub().throws();
try {
stub1();
} catch(e) {
console.log("stub1 throws");
}

// stub existing object

var engine2 = new App.Engine();
console.info("engine.getState method before creating stub returns", engine2.getState());
var stub2 = TypeSinon.stub(engine2);
console.info("engine.getState method after creating stub returns", engine2.getState());

// stub method on existing object

var engine3 = new App.Engine();
var stub3 = TypeSinon.stub(engine3, engine3.start).throws();

try {
engine3.start();
} catch(e) {
console.log("stub3 throws");
console.info("engine.getState method should still return state properly:", engine3.getState());
}

// stub method on existing object with given (as param) function

var engine4 = new App.Engine();
var fakeStartFunc = function() {
console.log("fake startEngine");
};
var stub4 = TypeSinon.stub(engine4, engine4.start, fakeStartFunc);
engine4.start();

var wrongFakePerformDutiesFunc = function (p: number): boolean {
console.log("wrong fake performDuties");
return true;
}

// below will not work, TS compiler will return following error:
// Type argument candidate '() => void' is not a valid type argument because it is not a supertype of candidate '(p: number) => boolean'.

// TypeSinon.stub(engine4, engine4.start, wrongFakePerformDutiesFunc);
// engine4.start();