Post undefined in the http mock unit testing in angular 2 -
i new angular2 , unit testing. writing unit testing component , mock service.
associate component.ts
onsubmit() { if (this.form.valid) { const data: associatepostform = { associatetoken: (this.form.value.tokennumber).touppercase() }; this.associatevehicleservice.vehicleassociate(data) .subscribe( success => { this.loginservice.vehicleattributes({'associatedtokens': [success.associationtoken]}).subscribe( result => { sessionstorage.setitem('vehicleattributes', json.stringify(result)); this.commonservice.menu(sessionstorage.setitem('menushow', 'true')); this.router.navigate(['product-catalog']); }, error => { console.log(error); } ); }, error => { console.log(error); } ); } }
associate service.ts
vehicleassociate(data){ return this.httpclient.post(this.httpclient.api_endpoint + 'uservehicleassociation', data, {}, { 'accept': 'application/json', 'content-type': 'application/x-www-form-urlencoded' }); }
http.service.ts
post(url, data, iparams = {}, iheaders = {}) { this.loaderservice.show(); return this.http .post(url, data, this.options(params, headers)) .map(this.transformresponse) .catch(this.handleerror) .finally(() => { this.loaderservice.hide(); }); }
associate-vehicle.component.spec.ts
it('on submit of form', inject([associatevehicleservice, mockbackend], (assocatevehicleservice, backend) => { expect(component.form.valid).tobefalsy(); component.form.controls['tokennumber'].setvalue('1m8gdm9apra033333'); expect(component.form.valid).tobetruthy(); backend.connections.subscribe((connection: mockconnection) => { let options = new responseoptions({ body: json.stringify({associatetoken: '1m8gdm9akp042755'}) }); connection.mockrespond(new response(options)); }); component.onsubmit(); assocatevehicleservice.vehicleassociate({associatetoken: '1m8gdm9apra011111'}) .subscribe((respone) => { expect(respone.json()).toequal({associatetoken: '1m8gdm9akp042755'}); }); }));
and error has cannot read property 'post' of undefined
insight highly appreciated.
thanks in advance.
Comments
Post a Comment