我正在测试一个使用authService的组件,并且内部有一个方法可以调用另一个服务并返回一个可观察的布尔值。我试图覆盖该方法,并在调用该方法时简单地返回true。
我尝试了以下方法,但是实际的方法正在被调用。
it('should', () => {
authService.method = jasmine.createSpy().and.returnValue(true)
fixture.detectChanges()
component.onSubmit();
expect(component.errorMessage).toEqual('error');
})
答案 0 :(得分:0)
尝试:
import { of } from 'rxjs';
....
it('should set errorMessage', () => {
spyOn(authService, 'method').and.returnValue(of(true));
component.onSubmit();
expect(component.errorMessage).toEqual('error');
});