Angular 在单元测试期间观察变量的变化

Angular 在单元测试期间观察变量的变化,angular,jasmine,angular-unit-test,Angular,Jasmine,Angular Unit Test,我正在开发一个组件,该组件主要在onNgInit()方法上执行其工作: 现在在我的测试中,stage的值仍然是“start”,因为我相信代码是onNgInit。我想知道是否有一种方法可以观察变量的值的变化,或者更好的是,观察特定变量的最终值 import { async, ComponentFixture, TestBed } from "@angular/core/testing"; import { TrackerComponent } from "./tracker.component";

我正在开发一个组件,该组件主要在onNgInit()方法上执行其工作:

现在在我的测试中,stage的值仍然是“start”,因为我相信代码是onNgInit。我想知道是否有一种方法可以观察变量的值的变化,或者更好的是,观察特定变量的最终值

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

  });

});
import{async,ComponentFixture,TestBed}来自“@angular/core/testing”;
从“/tracker.component”导入{TrackerComponent};
描述(“TrackerComponent”,()=>{
let组件:TrackerComponent;
let夹具:组件夹具;
beforeach(异步(()=>{
TestBed.configureTestingModule({
声明:[跟踪组件]
}).compileComponents();
}));
在每个之前(()=>{
fixture=TestBed.createComponent(TrackerComponent);
组件=fixture.componentInstance;
fixture.detectChanges();
});
//
它(“应该被创建”,()=>{
expect(component.toBeTruthy();
expect(component.stage).toEqual('start');//要跟踪更改吗
});
});

您需要同步对
uniService.dataExists()的异步调用。然后
stockService.productTypes()。在使用
exec
检查条件之前订阅
。这可以使用第二个
beforeach
函数中的
fakeAsync
flush
来完成

import { fakeAsync, flush } from '@angular/core/testing';
...
beforeEach(fakeAsync(() => {
    fixture = TestBed.createComponent(TrackerComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
    flush();
}));

我真的不明白为什么当它是ngOnInit时你叫它onNgInit,但这里有一个答案,假设它是a型:

您需要重写一点ngOnInit以返回某种值

  public async ngOnInit(): Promise<void>{
      const thus : any = this;
      this.stage = "start';
      const result = await this.uniService.dataExists();
       // the data exists. get ProductTypes
      const proTypes = await (that.stockService.productTypes().toPromise());
      this.stage = "Setting status to products";
      proTypes.map(product....);
  }
要么如此,要么从承诺转向可观察


你也不应该只相信函数的执行。测试时,您应该测试每个停止点及其进程。

您必须在断言之前调用
组件.onNgInit
。我真的不明白您想做什么/问题出在哪里。您可以重新表述一下帖子吗?@user3875919:
fixture.detectChanges
触发组件的更改检测循环,并隐式调用其
ngOnInit
方法,无需再次显式调用。我认为这不是
ngOnInit
而是
onngonit
  public async ngOnInit(): Promise<void>{
      const thus : any = this;
      this.stage = "start';
      const result = await this.uniService.dataExists();
       // the data exists. get ProductTypes
      const proTypes = await (that.stockService.productTypes().toPromise());
      this.stage = "Setting status to products";
      proTypes.map(product....);
  }
 it("should is created", (done) => {
    expect(component.stage).toEqual('start');
    component.ngOnInit().then(() =>{
        expect(component.stage).toEqual('Setting status to products');          
        done();
    });
  });