En febrero de 2017, fusionaron un PR agregando esta función, que lanzaron en abril de 2017.
así que para espiar los getters / setters que usa:
const spy = spyOnProperty(myObj, 'myGetterName', 'get');
donde myObj es su instancia, 'myGetterName' es el nombre del que está definido en su clase como get myGetterName() {}
y el tercer parámetro es el tipo get
o set
.
Puede usar las mismas afirmaciones que ya usa con los espías creados con spyOn
.
Entonces puedes, por ejemplo:
const spy = spyOnProperty(myObj, 'myGetterName', 'get'); // to stub and return nothing. Just spy and stub.
const spy = spyOnProperty(myObj, 'myGetterName', 'get').and.returnValue(1); // to stub and return 1 or any value as needed.
const spy = spyOnProperty(myObj, 'myGetterName', 'get').and.callThrough(); // Call the real thing.
Aquí está la línea en el código fuente de github donde este método está disponible si está interesado.
https://github.com/jasmine/jasmine/blob/7f8f2b5e7a7af70d7f6b629331eb6fe0a7cb9279/src/core/requireInterface.js#L199
Respondiendo a la pregunta original, con jazmín 2.6.1, haría lo siguiente:
const spy = spyOnProperty(myObj, 'valueA', 'get').andReturn(1);
expect(myObj.valueA).toBe(1);
expect(spy).toHaveBeenCalled();
valueA
es unObservable
oSubject
? Estoy recibiendoProperty valueA does not have access type get