Демонстрация Stackblitz
Для отслеживания свойств получателя / установщика вы можете использовать jasmine spyOnProperty :
![enter image description here](https://i.stack.imgur.com/2lsDH.png)
Итак, в вашем примере мы имеем: obj -> component, propertyName -> 'small', accessType -> 'set'
:
it('call small setter method', () => {
const small = spyOnProperty(component, 'small', 'set'); // just spyOn setter method of small
component.size = 10; // set initial value of size to 10
component.small = 'large'; // set small property which as expected - should call setted method
// we expect that `small` setter method will be called
// with 'large' argument, since we setted it before
expect(small).toHaveBeenCalledWith('large');
// we also expect, that the logic of your setter method works correctly: `this.size = !value ? 25 : this.size;`
// since we pass value, your `this.size` will not change
expect(component.size).toBe(10);
});
Редактировать
Это еще один тест, если мы передаем аргументу-установщику пустую строку:
it('call small setter method 2', () => {
const small = spyOnProperty(component, 'small', 'set').and.callThrough();
component.small = '';
expect(small).toHaveBeenCalledWith('');
expect(component.size).toBe(25);
});
Как и ожидалось - установщик small
будет вызываться с пустой строкой ', а свойство size
будет иметь значение 25 из-за этого: this.size = !value ? 25 : this.size;