业力:属性没有访问类型获取 [英] Karma: property does not have access type get
问题描述
我的Angular应用程序的Karma测试中出现错误.错误是当我运行测试时:
I have an error in my Karma tests in my Angular application. The error is when I run my tests:
Failed: Property activePropertyChanged does not have access type get
我正在尝试模拟名为ModuleSpecService
的服务.在此服务中,有以下吸气剂:
I'm trying to mock a service called ModuleSpecService
. In this service there's the following getter:
get activePropertyChanged(): Observable<SpecificationPropertyObject> {
return this.activePropChangedSubject.asObservable();
}
在我的spec
文件中,我像这样模拟它:
And in my spec
file I mock it like this:
spyOnProperty(moduleSpecServiceMock, 'activePropertyChanged', 'get').and.returnValue(of());
// then, in configureTestingModule() I define/mock the service like this:
providers: [{ provide: ModuleSpecService, useValue: moduleSpecServiceMock }]
因此,我的服务中显然有一个吸气剂,我想嘲笑它.如果我用spyOnProperty()
删除行,则会引发以下错误:
So there's clearly a getter in my service which I want to mock. If I remove the line with spyOnProperty()
it throws the following error:
TypeError: this.moduleSpecService.activePropertyChanged.subscribe is not a function
所以我绝对需要这个模拟游戏.
so I definitely need the mock.
有什么想法会出问题吗?
Any idea what could go wrong?
推荐答案
由于这似乎是茉莉花中的错误,所以我设法通过一种解决方法来解决此问题:
Since this seems to be a bug in jasmine I managed to fix this with a workaround:
代替此:
spyOnProperty(moduleSpecServiceMock, 'activePropertyChanged', 'get').and.returnValue(of());
我这样定义属性:
(moduleSpecServiceMock as any).activePropertyChanged = of();
我不得不将其强制转换为any
,因为如果没有,它(正确)告诉我activePropertyChange
是read-only
属性(因为它只有一个吸气剂).
I had to cast it as any
, because if not, it (correctly) told me that activePropertyChange
is a read-only
property (since it has only a getter).
不是最好的解决方案,但至少可以起作用:)
Not the best solution, but at least it works :)
这篇关于业力:属性没有访问类型获取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!