Karma:属性没有访问类型 get

dav*_*688 17 jasmine karma-runner angular

我的 Angular 应用程序中的 Karma 测试出错。错误是当我运行测试时:

Failed: Property activePropertyChanged does not have access type get

我正在尝试模拟一个名为ModuleSpecService. 在此服务中有以下 getter:

get activePropertyChanged(): Observable<SpecificationPropertyObject> {
    return this.activePropChangedSubject.asObservable();
}
Run Code Online (Sandbox Code Playgroud)

在我的spec文件中,我像这样模拟它:

spyOnProperty(moduleSpecServiceMock, 'activePropertyChanged', 'get').and.returnValue(of());

// then, in configureTestingModule() I define/mock the service like this:
providers: [{ provide: ModuleSpecService, useValue: moduleSpecServiceMock }]
Run Code Online (Sandbox Code Playgroud)

所以我的服务中显然有一个我想嘲笑的吸气剂。如果我删除带有spyOnProperty()它的行会引发以下错误:

TypeError: this.moduleSpecService.activePropertyChanged.subscribe is not a function

所以我绝对需要模拟。

知道会出什么问题吗?

dav*_*688 27

由于这似乎是 jasmine 中的一个错误,我设法通过一种解决方法解决了这个问题:

取而代之的是:

spyOnProperty(moduleSpecServiceMock, 'activePropertyChanged', 'get').and.returnValue(of()); 
Run Code Online (Sandbox Code Playgroud)

我定义了这样的属性:

(moduleSpecServiceMock as any).activePropertyChanged = of();
Run Code Online (Sandbox Code Playgroud)

我不得不将它转换为any,因为如果没有,它(正确地)告诉我这activePropertyChange是一个read-only属性(因为它只有一个吸气剂)。

不是最好的解决方案,但至少它有效:)