Javascript jasmine测试覆盖服务方法
我正在测试一个使用authService的组件,其中有一个方法调用另一个服务并返回一个可观察的布尔值。我试图重写该方法,并在调用时简单地返回true 我已经尝试了下面的方法,但是正在调用实际的方法Javascript jasmine测试覆盖服务方法,javascript,angular,jasmine,Javascript,Angular,Jasmine,我正在测试一个使用authService的组件,其中有一个方法调用另一个服务并返回一个可观察的布尔值。我试图重写该方法,并在调用时简单地返回true 我已经尝试了下面的方法,但是正在调用实际的方法 it('should', () => { authService.method = jasmine.createSpy().and.returnValue(true) fixture.detectChanges() component.onSubmit(); expect(com
it('should', () => {
authService.method = jasmine.createSpy().and.returnValue(true)
fixture.detectChanges()
component.onSubmit();
expect(component.errorMessage).toEqual('error');
})
尝试:
您在哪里传递模拟身份验证服务,它是依赖注入的吗?
import { of } from 'rxjs';
....
it('should set errorMessage', () => {
spyOn(authService, 'method').and.returnValue(of(true));
component.onSubmit();
expect(component.errorMessage).toEqual('error');
});