lundi 21 octobre 2019

ActivatedRoute params not working in unit test when writing more than one test case

I'm mocking the params value of ActivatedRoute using the following code:

 providers: [
    {
      provide: ActivatedRoute,
      useValue: { params: of({id: 1})}
    },

My component uses the ActivateRoute as this:

ngOnInit() {
this.route.params.subscribe(params => {
  this.selectedId = +params['id'];

My testCases are currently empty. The first one succeeds, the second one fails.

describe('The VIS form', () => {
  it('should be invalid', () => {
   //Nothing here
  });

  it('should be valid', () => {
   //Nothing here
  });
});

It's giving me the following error ONLY at the should be valid test case. the should be invalid test case works as expected:

TypeError: Cannot read property 'subscribe' of undefined
at SafeSubscriber.route.params.subscribe.

I've tried mocking the ActivatedRoute using a stub, using a subject as a source for Params.

I expect both tests to pass, since they're both empty and do nothing.

See image for the test results

Aucun commentaire:

Enregistrer un commentaire