我有一个离子项目,在组件的构造函数中包含一个根据条件调用的方法。我正在使用 Jasmine,我想监视该方法以检查它是否被调用。
这是组件的构造函数:
export class MyComponent {
public test: boolean;
constructor(public service: MyService) {
if(test) {
service.getX();
}
}
}
Run Code Online (Sandbox Code Playgroud)
在我的spec.ts中,我必须实例化该组件才能监视该方法,但由于该方法已在构造函数中被调用,所以不起作用。
beforeEach(() => {
fixture = TestBed.createComponent(MyComponent);
component = fixture.componentInstance;
});
it('Test case #1', () => {
component.test = true;
spyOn(component.service, 'getX'); //The method has already been called in the constructor called in beforEach above
expect(component.service.getX).toHaveBeenCalled();
})
Run Code Online (Sandbox Code Playgroud)
如何监视构造函数中调用的方法?