fix(tests): create default spys for all methods on a class
This commit is contained in:
parent
fef1dee7aa
commit
cb2e646332
|
@ -272,6 +272,16 @@ _global.beforeEach(function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
export class SpyObject {
|
export class SpyObject {
|
||||||
|
constructor(type = null) {
|
||||||
|
if (type) {
|
||||||
|
for (var prop in type.prototype) {
|
||||||
|
var m = type.prototype[prop];
|
||||||
|
if (typeof m === 'function') {
|
||||||
|
this.spy(prop);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
spy(name){
|
spy(name){
|
||||||
if (! this[name]) {
|
if (! this[name]) {
|
||||||
this[name] = this._createGuinnessCompatibleSpy();
|
this[name] = this._createGuinnessCompatibleSpy();
|
||||||
|
@ -286,6 +296,8 @@ export class SpyObject {
|
||||||
_createGuinnessCompatibleSpy(){
|
_createGuinnessCompatibleSpy(){
|
||||||
var newSpy = jasmine.createSpy();
|
var newSpy = jasmine.createSpy();
|
||||||
newSpy.andCallFake = newSpy.and.callFake;
|
newSpy.andCallFake = newSpy.and.callFake;
|
||||||
|
// return null by default to satisfy our rtts asserts
|
||||||
|
newSpy.and.returnValue(null);
|
||||||
return newSpy;
|
return newSpy;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,11 +7,17 @@ class TestObj {
|
||||||
constructor(prop) {
|
constructor(prop) {
|
||||||
this.prop = prop;
|
this.prop = prop;
|
||||||
}
|
}
|
||||||
|
someFunc():number {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@proxy
|
@proxy
|
||||||
@IMPLEMENTS(TestObj)
|
@IMPLEMENTS(TestObj)
|
||||||
class SpyTestObj extends SpyObject {noSuchMethod(m){return super.noSuchMethod(m)}}
|
class SpyTestObj extends SpyObject {
|
||||||
|
constructor(){super(TestObj);}
|
||||||
|
noSuchMethod(m){return super.noSuchMethod(m)}
|
||||||
|
}
|
||||||
|
|
||||||
export function main() {
|
export function main() {
|
||||||
describe('test_lib', () => {
|
describe('test_lib', () => {
|
||||||
|
@ -66,11 +72,23 @@ export function main() {
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should record function calls", () => {
|
it("should record function calls", () => {
|
||||||
spyObj.spy("someFunc").andCallFake((a,b) => a + b);
|
spyObj.spy("someFunc").andCallFake((a,b) => {
|
||||||
|
return a + b
|
||||||
|
});
|
||||||
|
|
||||||
expect(spyObj.someFunc(1,2)).toEqual(3);
|
expect(spyObj.someFunc(1,2)).toEqual(3);
|
||||||
expect(spyObj.spy("someFunc")).toHaveBeenCalledWith(1,2);
|
expect(spyObj.spy("someFunc")).toHaveBeenCalledWith(1,2);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should create spys for all methods', () => {
|
||||||
|
expect(spyObj.someFunc).toBeTruthy();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should create a default spy that does not fail for numbers', () => {
|
||||||
|
// Need to return null instead of undefined so that rtts assert does
|
||||||
|
// not fail...
|
||||||
|
expect(spyObj.someFunc()).toBe(null);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue