How to mock an activatedRoute parent Route in angular2 for testing purposes?

后端 未结 6 518
渐次进展
渐次进展 2020-12-05 03:57

Let\'s say I have this

export class QuestionnaireQuestionsComponent {

    questions: Question[] = [];
    private loading:boolean = true;


    constructor(         


        
6条回答
  •  不知归路
    2020-12-05 05:01

    To customize a mocked ActivatedRoute's data inside each 'it' block, combine what Kevin Black suggested above

    beforeEach(async(() => {
        TestBed.configureTestingModule({
          declarations: [YourComponent],
          imports: [],
          providers: [
            {
              provide: ActivatedRoute, useValue: {
                queryParams: of({ id: 'test' })
              }
            }
          ]
        })
          .compileComponents();
      }));

    and the below code in the it('') block before instantiating a component using fixture.detectChanges()

    it('test', fakeAsync(() => {
      let activatedRoute: ActivatedRoute = fixture.debugElement.injector.get(ActivatedRoute);
      activatedRoute.queryParams = of({ firstName: 'john', lastName: 'smith' });
    
      fixture.detectChanges(); // trigger ngOninit()
      tick();
    }));

提交回复
热议问题