【发布时间】:2020-03-11 13:27:02
【问题描述】:
我正在开发一个主要在 onNgInit() 方法上完成其工作的组件:
stage = '';
onNgInit(){
const thus : any = this;
this.stage = "start';
this.uniService.dataExists().then(result=>{
// the data exists. get ProductTypes
that.stockService.productTypes().subscribe(proTypes=>{
vm.stage = "Setting status to products";
proTypes.map(product....);
});
});
}
现在在我的测试中,我相信,因为代码是 onNgInit,stage 的值仍然是“start”。我想知道是否有办法观察变量的值变化,或者更好的是,特定变量的最终值?
import { async, ComponentFixture, TestBed } from "@angular/core/testing";
import { TrackerComponent } from "./tracker.component";
describe("TrackerComponent", () => {
let component: TrackerComponent;
let fixture: ComponentFixture<TrackerComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [TrackerComponent]
}).compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(TrackerComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
//
it("should is created", () => {
expect(component).toBeTruthy();
expect(component.stage).toEqual('start');// want to track the changes
});
});
【问题讨论】:
-
你必须在断言之前调用
component.onNgInit。 -
我真的不明白你想做什么/问题是什么。你能改写这篇文章吗?
-
@user3875919:
fixture.detectChanges触发组件的变更检测循环并隐式调用其ngOnInit方法,无需再次显式调用。 -
我以为这不是
ngOnInit,而是onNgInit
标签: angular jasmine angular-unit-test