隨着二傳手(set
),你要做的就是將值賦給屬性(方法)
let fixture = TestBed.createComponent(TestComponent);
let component = fixture.componentInstance;l
component.isRunning = true;
fixture.detectChanges();
對於超時,你可能需要做一些像
import { fakeAsync } from '@angular/core/testing;
it('should change isDelayedRunning', fakeAsync(() => {
let fixture = TestBed.createComponent(TestComponent);
let component = fixture.componentInstance;
fixture.detectChanges();
component.isRunning = true;
// wait for timeout
tick(200);
fixture.detectChanges();
expect(fixture.componentInstance.isDelayedRunning).toBe(true);
}));
fakeAsync
如果您在組件中使用templateUrl
,將無法使用。所以你必須使用async
。但是AFAIK沒有像tick
這樣的設施,我們可以控制等待期,所以你可能不得不在測試中設定一個超時時間
import { async } from '@angular/core/testing';
it('should change isDelayedRunning', async(() => {
let fixture = TestBed.createComponent(TestComponent);
let component = fixture.componentInstance;
fixture.detectChanges();
component.isRunning = true;
setTimeout(() => {
fixture.detectChanges();
expect(fixture.componentInstance.isDelayedRunning).toBe(true);
}, 200);
}));