Skip to content

Instantly share code, notes, and snippets.

@lydemann
Last active April 21, 2018 10:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lydemann/67758e852ef1179e85862c890a1de271 to your computer and use it in GitHub Desktop.
Save lydemann/67758e852ef1179e85862c890a1de271 to your computer and use it in GitHub Desktop.
Provides helpers for easy and typesafe mocking with spies
/*
This class provides helpers for easy and typesafe mocking with spies
*/
// get keys of type
export declare function keys<T extends object>(): Array<keyof T>;
export class SpyHelper {
/*
Ensures typesafe creation of a spy object
*/
public static createSpyObj<T>(name: string = 'spyObject', methods: Array<keyof T>): jasmine.SpyObj<T> {
return jasmine.createSpyObj<T>(name, methods);
}
public static createSpyObjFromObj<T>(name: string = 'spyObject', obj: T): jasmine.SpyObj<T> {
const keysOfObject = [];
for (const key in obj) {
if (obj.hasOwnProperty(key)) {
keysOfObject.push(key);
}
}
return jasmine.createSpyObj<T>(name, keysOfObject);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment