vendredi 30 mars 2018

Test subscribing to Location in angular 2 with karma+jasmine (this.location.subscribe)

I am subscribing to the angular Location service in my component as such:

this.location.subscribe((ev:PopStateEvent) => {
    this.lastPoppedUrl = ev.url;
});

I'd like to be able to test it along with the rest of my component.

Right now I have this stub in my component.spec.ts file

let locationStub: Partial<Location>;
    locationStub = {
}

and am configuring it into my testbed as a provider:

{provide: Location, useValue: locationStub }

When I run ng test I get this error this.location.subscribe is not a function.

How can I create a stub or spy that will allow me to pass the .subscribe functionality of Location.

Here is a similar question on testing Location, but it is referring to functions within Location, not specifically subscribing to Location.

Any help is much appreciated.

Aucun commentaire:

Enregistrer un commentaire