While working with Jasmine 2.9, I have encountered no issues spying on both public and private functions, except for when trying to spy on a get
or set
function at the class level.
private class RandomService {
public dogsHealth = 0;
private get personsFullName(): string {
return firstName + lastname;
}
private set personsLocation(address: string, city: string, country: string): string {
return address + city + country;
}
public get dogsFullName(): string {
return dogFirstName + dogLastName;
}
public get isDogAlive(): boolean {
return dogsHealth <= 0 ? true : false;
}
}
I have attempted various solutions:
spyOnProperty(RandomService, 'dogsFullName', 'get').and.returnValue(true);
spyOnProperty(RandomService, 'dogsFullName').and.returnValue(true);
spyOn(RandomService, 'dogsFullName').and.returnValue(true);
spyOnProperty(RandomService.dogsFullName, 'dogsFullName', 'get').and.returnValue(true);
Despite my efforts, I have not found a solution online and will continue to search. The get or set functions create variables, so I thought solution 4 might work, but it did not.
Update
(The code above has been updated as well)
Attempting to update by returning a string and using the following jasmine method results in an error:
spyOnProperty(RandomService, 'dogsFullName', 'get').and.returnValue('Frank');
Expected a spy, but got 'Frank'
Similarly, for the function isDogAlive
, I receive the following message:
<toHaveBeenCalled> : Expected a spy, but got true.
While I understand that the correct value is being returned, shouldn't it be recognized as a spy if I use spyOn
?