Angular2, testing and resolved data: How to test ngOnInit? Angular2, testing and resolved data: How to test ngOnInit? angular angular

Angular2, testing and resolved data: How to test ngOnInit?


What is the behavior or the ngOnInit method? All it does is assign the value of the org when the route data is resolved. So that's all you really need to test.

let routeStub;beforeEach(() => {  routeStub = {    data: null  }  TestBed.configureTestingModule({    providers: [      { provide: ActivatedRoute, useValue: routeStub }      ]  })})it('should assign org when route is resolved', async(() => {  let org = new Org()  routeStub.data = Observable.of(org)  fixture.detectChanges();  fixture.whenStable().then(() => {    expect(component.org).toEqual(org)  })}))


I was trying to test ngOnInit() for a component, and unfortunately the accepted answer didn't work for me. However, this did:

describe('your test', () => {  beforeEach(async() => {    // set up your component as necessary    component.ngOnInit();    await fixture.whenStable();  });  it('should verify your test', () => {    // run your expectation  });});