Karma: property does not have access type get
Solution 1:
Since this seems to be a bug in jasmine I managed to fix this with a workaround:
Instead of this:
spyOnProperty(moduleSpecServiceMock, 'activePropertyChanged', 'get').and.returnValue(of());
I defined the property like this:
(moduleSpecServiceMock as any).activePropertyChanged = of();
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 :)
Solution 2:
You can use the spread syntax:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax
const moduleSpecServiceMock = {
...jasmine.createSpyObj('moduleSpecServiceMock ', ['']),
activePropertyChanged: of()
} as jasmine.SpyObj;
This allows you to set private properties and getters without the need for as any
syntax.
One great benefit of this is that if you refactor the getter name then the test property will also update, with the as any
syntax you lose the typings and ability to refactor.
Solution 3:
I fixed that error, creating a get
property in the service, so:
.spec.ts
file:
spyOnProperty(moduleSpecServiceMock, 'activePropertyChanged', 'get').and.returnValue(of("Test"));
.service.ts
file:
get activePropertyChanged(): Observable<any> {
return of({...element});
}
Solution 4:
I found a slightly different solution to this problem:
Object.defineProperty(moduleSpecServiceMock, 'activePropertyChanged', { value: of() });
enter code here
this solution even rewrites private values