tymspy tymspy - 4 months ago 12
AngularJS Question

How do you test a function which is called with a constant using jasmine + TypeScript

I'm working on a Angular2 / TypeScript Project and using jasmine for unit-testing.

How to test a function which is called with a constant using jasmine.
Eg.
Logo.ts

export const RADIUS: number = 10;

export class Logo {
...
protected drawCircle( x: number, y: number, r: number ){...}

protected drawLogo(){
this.drawCircle( RADIUS, RADIUS, RADIUS );
}
...
}


Logo.spec.ts

describe('drawLogo', function () {
beforeEach(() => {
spyOn( logo, 'drawCircle');
logo.drawLogo()
}
it('should call drawCircle method with parameters'){
expect( logo.drawCircle ).toHaveBeenCalledWith( 10, 10, 10 ); //This fails
}
}


Is there any other way to test other than passing the constant as parameter to toHaveBeenCalledWith method ?

Answer

you need to use a spy first

spyOn('logo','drawCircle');
logo.drawLogo();
 expect( logo.drawCircle ).toHaveBeenCalledWith( 10, 10, 10 );
Comments