Unit testing for Angular 2 using Jasmine: "Expected spy fails on toHaveBeenCalled" -
as in title test failing on expectation of method have been called.
part of component:
@input('addressformcontrol') addressformcontrol: formcontrol; public ngoninit() { this.addressformcontrol .valuechanges .debouncetime(500) .subscribe(this.showaddresslistfound); } test:
@component({ selector: 'wrapper-form-check-id', template: ` <div> <predictor-list-addresses [addressformcontrol]="formctrl"></predictor-list-addresses> </div> ` }) class wrapformcheckidcomponent { public formctrl: formcontrol = new formcontrol(''); } fdescribe('form-id', () => { describe('in predictorlistaddressescomponent', () => { let fixture: componentfixture<predictorlistaddressescomponent>; let component: predictorlistaddressescomponent; let wrapcomponent: wrapformcheckidcomponent; let mockresponsepredictorretrieveaddress = {}; let mockresponsepredictorfindaddress = {}; let addressservicemock = jasmine.createspyobj('addressservice', ['getlistaddresses', 'getaddressdetails']); addressservicemock.getlistaddresses.and.returnvalue(observable.of(mockresponsepredictorfindaddress)); addressservicemock.getaddressdetails.and.returnvalue(observable.of(mockresponsepredictorretrieveaddress)); beforeeach( () => { testbed.configuretestingmodule({ declarations: [ wrapformcheckidcomponent, predictorlistaddressescomponent, makeitbolddirective ], imports : [reactiveformsmodule], providers : [{provide: addressservice, usevalue: addressservicemock}] }); wrapcomponent = testbed.createcomponent(wrapformcheckidcomponent).componentinstance; fixture = testbed.createcomponent(predictorlistaddressescomponent); component = fixture.componentinstance; component.addressformcontrol = wrapcomponent.formctrl; spyon(component, 'showaddresslistfound'); fixture.detectchanges(); }); it('should defined', () => { expect(fixture).tobedefined(); }); it('"addressformcontrol: formcontrol" should listening value changes, calling "showaddresslistfound(value)" right value', () => { // spyon(component, 'showaddresslistfound').and.returnvalue(observable.of(component.addressformcontrol.value)); wrapcomponent.formctrl.setvalue('street 102'); fixture.whenstable().then(() => { expect(component.addressformcontrol.value).tobe('street 102'); expect(component.showaddresslistfound).tohavebeencalled(); }); }); am doing wrong creating spy 'showaddresslistfound'?
the value check working though.
Comments
Post a Comment