javascript - Angular 2 unit test throwing error 'subscribe' is not a member of undefined -


i'm working on jasmine unit tests in angular 2 app.

it('cancel object passed cancellation service in expected format cancel contract transaction', () => {     var cancelreason = new models.cancelreason();     cancelreason.cancelreasoncode = '165';     cancelreason.cancelreasondescription = 'because said so';      component.subscriptioncancelreasons = [];     component.subscriptioncancelreasons.push(cancelreason);      component.cancellationsmodel = new models.cancellationsmodel();     component.cancellationsmodel.groupnumber = 'xxxxxx';     component.cancellationsmodel.billingunit = 'xxxx';     component.cancellationsmodel.memberid = 'xxxxxxxxx';     component.cancellationsmodel.cancellationdate = '01/01/2020';     component.cancellationsmodel.cancellationreason = '165';      component.cancellationsmodel.cancelcontract = true;      spyon(cancelservice, 'cancel');     component.cancel(); // initiate cancel      expect(cancelservice.cancel).tohavebeencalledwith({         memberkey: '000',         groupnumber: 'xxxxxx',         billingunit: 'xxxx',         certificatenumber: 'xxxxxxxxx',         effectivedate: '01/01/2020',         cancelreason: '165'     }); }); 

this throwing error:

typeerror: unable property 'subscribe' of undefined or null reference

here's cancelservice.cancel function:

cancel(cancelobject: any): {     this.log('cancel', 'cancelobject: ' + json.stringify(cancelobject));      var contractsurl = environment.contractsserviceurl + '/contracts/cancel';      return this._http.put(contractsurl, cancelobject)         .map(response => this.extractcancelresponse(response))         .catch(this.handleerror); } 

and here's controller/component actual cancel call:

private executecancel(transactiontype:string, cancelobject: any) {     this.log('executecancel', 'executing cancel transaction...');      this.cancelservice.cancel(cancelobject)         .subscribe(response => {             this.parsecancellationresponse(transactiontype, response, cancelobject, true);         }, error => {             this.parsecancellationresponse(transactiontype, error, cancelobject, false);         }); } 

i have many other tests in spec file work great, first test i've tried write verify data gets sent service.

what's wrong implementation? if subscribe works when running app, why unit test fail? don't care [yet] if service makes call successfully, want make sure object cancel function [in cancel service] receives expected.

edit (in response answer user8019820):

sorry, should have included 2 beforeeach functions:

let component: contractcancellationscomponent; let fixture: componentfixture<contractcancellationscomponent>; let cancelservice: cancelservice;  beforeeach(async(() => {     testbed.configuretestingmodule({         imports: [             httpmodule,             formsmodule,             routertestingmodule         ],         providers: [             appdataservice,             errorservice,             cancelservice,             contractsservice         ],         declarations: [             contractcancellationscomponent,             wmheadercomponent,             wmerrorlistcomponent,             wmstatusmessagecomponent,             wmspinnercomponent,             wmtimeoffsetpipe         ]     }).compilecomponents(); }));  beforeeach(() => {     fixture = testbed.createcomponent(contractcancellationscomponent);     component = fixture.componentinstance;     cancelservice = fixture.debugelement.injector.get(cancelservice);     fixture.detectchanges(); }); 

if use service in test, have declare in providers of testbed , instantiate in beforeeach() function


Comments

Popular posts from this blog

javascript - Create a stacked percentage column -

Optimising Firebase database by automatically overwriting data -

javascript - Angular UI-Grid customTemplate directive causing rows to load slowly/? -