Angular 重构Observable以避免类型断言错误

Angular 重构Observable以避免类型断言错误,angular,jasmine,Angular,Jasmine,我正在为CanDeactive Guard编写单元测试,我的jasmine规范中出现了一个类型断言错误: // Mock Guard defined at top of spec file class MockGuardComponent implements ComponentCanDeactivate { // Set this value to the value you want to mock being returned from GuardedComponent retu

我正在为CanDeactive Guard编写单元测试,我的jasmine规范中出现了一个类型断言错误:

// Mock Guard defined at top of spec file
class MockGuardComponent implements ComponentCanDeactivate {
  // Set this value to the value you want to mock being returned from 
GuardedComponent
  returnValue: boolean | Observable<boolean>;

  canDeactivate(): boolean | Observable<boolean> {
    return this.returnValue || this.openConfirmDialog();
  }
}

it('will not route if guarded and user rejected the dialog', () => {
    // Mock the behavior of the MockGuardedComponent
    const subject$ = new Subject<boolean>();
    mockGuardComponent.returnValue = subject$.asObservable();
    const canDeactivate$ = <Observable<boolean>>(
      service.canDeactivate(mockGuardComponent)
    );
    canDeactivate$.subscribe((deactivate) => {
      // this is the real test
      expect(deactivate).toBeFalsy();
    });
    // Emulate the reject
    subject$.next(false);
//在spec文件顶部定义了Mock-Guard
类MockGuardComponent实现ComponentCanDeactivate{
//将此值设置为要模拟从中返回的值
保护元件
返回值:布尔值|可观察;
canDeactivate():布尔值|可观察{
返回this.returnValue | | this.openConfirmDialog();
}
}
它('如果受保护且用户拒绝对话框,则不会路由',()=>{
//模拟MockGuardedComponent的行为
const subject$=新主题();
mockGuardComponent.returnValue=subject$.asObservable();
常数canDeactivate$=(
service.canDeactivate(mockGuardComponent)
);
canDeactivate$.subscribe((停用)=>{
//这是真正的考验
expect(停用).toBeFalsy();
});
//仿效拒绝
主题$.next(假);
})

此规范正确抛出以下错误:

Type assertion using the '<>' syntax is forbidden. Use the 'as' syntax instead.
禁止使用“”语法的类型断言。改用“as”语法。 它不喜欢这个部分:

<Observable<boolean>>

我知道使用行为主体可能更好,但我已经在使用主体,所以我不确定如何结合我在这里所做的。有什么建议吗?

改变

const canDeactivate$ = <Observable<boolean>>(
      service.canDeactivate(mockGuardComponent)
    );
const canDeactivate$=(
service.canDeactivate(mockGuardComponent)
);

const canDeactivate$=service.canDeactivate(mockGuardComponent)作为可观察对象;
更改

const canDeactivate$ = <Observable<boolean>>(
      service.canDeactivate(mockGuardComponent)
    );
const canDeactivate$=(
service.canDeactivate(mockGuardComponent)
);

const canDeactivate$=service.canDeactivate(mockGuardComponent)作为可观察对象;

重构代码,使
canDeactivate
始终返回一个
可观察的
重构代码,使
canDeactivate
始终返回一个
可观察的