DiPix
DiPix

Reputation: 6083

How to mock ActivatedRoute from Angular 5 tests - error Cannot read property 'params' of undefined

This is my constructor with ActivatedRoute

  constructor(private route: ActivatedRoute) {
    this.claimId = +route.snapshot.params["claimId"];
  }

And I'm trying to mock this ActivateRoute in tests in that way:

beforeEach((() => {
    TestBed.configureTestingModule({
      declarations: [TransactionSummaryComponent],
      providers: [{
        provide: useClass: MockClaim
      }, {
        provide: ActivatedRoute, useValue: {
          params: Observable.of({ claimId: 1 })
        }
      }]
    });

But whilst building tests I receiving an error: TypeError: Cannot read property 'params' of undefined

Upvotes: 0

Views: 1594

Answers (1)

DiPix
DiPix

Reputation: 6083

I have changed they way of getting id:

 constructor(private route: ActivatedRoute) {
    this.route.params.subscribe(params => {
        this.claimId = +params["claimId"];
    });
 }

And it works now.

Upvotes: 2

Related Questions