Karma: property does not have access type get

15,621

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

I found a slightly different solution to this problem:

Object.defineProperty(moduleSpecServiceMock, 'activePropertyChanged', { value: of() });
enter code here

this solution even rewrites private values

Solution 3

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 4

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});
}
Share:
15,621
dave0688
Author by

dave0688

Lead Frontend developer / IT Architect with 11 years of professional working experience. I’ve been contributing to numerous projects so far, taking over positions of i.e. Lead Developer, Architect or PO. Usually my tasks consist of designing scalable architectures and implementing complex workflows, making them easily understandable for the users. Usually my home is the frontend, taking over the full life cycle of a project: From the first concept, prototype (code or design-only), over to the architecture design - followed by the implementation (which makes about 80% of my daily business). A close collaboration with the team is a must, whereas SCRUM has been a close companion for the last couple of years. Regarding the technology stack I mainly use Angular 9, TypeScript, Bootstrap on Frontend - and Java, Spring, Spring Boot, SQL on the backend. My experience as team lead (of international teams, up to 12 developers/project managers) helped me to lead projects of all different sizes to success.

Updated on June 06, 2022

Comments

  • dave0688
    dave0688 about 2 years

    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

    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();
    }
    

    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 }]
    

    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?