How to spyOn a value property (rather than a method) with Jasmine

前端 未结 10 2248
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-01 04:12

Jasmine\'s spyOn is good to change a method\'s behavior, but is there any way to change a value property (rather than a method) for an object? the code could be

10条回答
  •  执念已碎
    2020-12-01 04:24

    The best way is to use spyOnProperty. It expects 3 parameters and you need to pass get or set as a third param.

    Example

    const div = fixture.debugElement.query(By.css('.ellipsis-overflow'));
    // now mock properties
    spyOnProperty(div.nativeElement, 'clientWidth', 'get').and.returnValue(1400);
    spyOnProperty(div.nativeElement, 'scrollWidth', 'get').and.returnValue(2400);
    

    Here I am setting the get of clientWidth of div.nativeElement object.

提交回复
热议问题