我正在一个Angular2 / TypeScript项目上,并使用jasmine进行单元测试。
如何使用 Jasmine 测试用常量调用的函数。
例如。
徽标
export const RADIUS: number = 10;
export class Logo {
...
protected drawCircle( x: number, y: number, r: number ){...}
protected drawLogo(){
this.drawCircle( RADIUS, RADIUS, RADIUS );
}
...
}
徽标规格
describe('drawLogo', function () {
beforeEach(() => {
spyOn( logo, 'drawCircle');
}
it('should call drawCircle method with parameters'){
expect( logo.drawCircle ).toHaveBeenCalledWith( 10, 10, 10 ); //This fails
}
}
除了将常量作为参数传递给toHaveBeenCalledWith方法之外,还有其他测试方法吗?
最佳答案
你需要先使用 spy
spyOn('logo','drawCircle');
logo.drawLogo();
expect( logo.drawCircle ).toHaveBeenCalledWith( 10, 10, 10 );