Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

1.1K
Vistas
Karma: property does not have access type get

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?

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

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 :)

over 4 years ago · Santiago Trujillo Denunciar

0

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.

over 4 years ago · Santiago Trujillo Denunciar

0

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});
}
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda