angular - Cannot read property 'getListAddresses' of undefined (where undefined is a service) -
in component:
constructor(private addressservice: addressservice){} public showaddresslistfound(valaddr: string, container?: string) { if ( !!valaddr ) { if ( !this._prevaddressvalue || this._prevaddressvalue.trim() !== valaddr.trim() ){ this._prevaddressvalue = valaddr; this.addressservice.getlistaddresses(valaddr, container) .subscribe( (predictedlistaddress) => { this.predictedlistaddress = predictedlistaddress; this.isinputblur = false; }); } } } in service:
constructor(private myhttp: myhttp, private http: http) { } in configuration these below providers need:
let fixture: componentfixture<predictorlistaddressescomponent>; let component: predictorlistaddressescomponent; let element: htmlelement; let wrapcomponent: wrapformcheckidcomponent; let debugel: debugelement; let address = 'street 10'; let debouncetime = 500; function initaddress(_address: string) { wrapcomponent.formctrl.setvalue(_address); component.ngoninit(); tick(debouncetime); } beforeeach( () => { testbed.configuretestingmodule({ // declarations , imports ok providers : [ {provide: addressservice, useclass: addressservice}, {provide: myhttp, useclass: mockbackend}, {provide: http, useclass: mockbackend} ] }); wrapcomponent = testbed.createcomponent(wrapformcheckidcomponent).componentinstance; fixture = testbed.createcomponent(predictorlistaddressescomponent); component = fixture.componentinstance; component.addressformcontrol = wrapcomponent.formctrl; spyon(component, 'showaddresslistfound').and.callthrough(); spyon(component.addressservice, 'getlistaddresses').and.returnvalue(observable.of({mockresponsepredictorfindaddress})); fixture.detectchanges(); }); the actual test:
it('"addressformcontrol: formcontrol" should listening value changes, calling "showaddresslistfound(value)" right value', fakeasync( () => { initaddress(address); fixture.detectchanges(); fixture.whenstable().then( () => { expect(component.addressformcontrol.value).tobe(address); expect(!component._prevaddressvalue).tobe(true); expect(component.showaddresslistfound).tohavebeencalledwith(address); }); }) ); // i'm trying test showaddresslistfound uses service addressservice addressservice.getlistaddresses don't why jasmine doesn't see instance of service, thought because had inject service although saw in other working tests using useclass without other injection around test.
Comments
Post a Comment