Hi there, I'm encountering an issue and would appreciate some assistance. I need to test a function as shown in the code snippet below:
populateForm() {
this.activatedRoute.params.subscribe(
params => {
this.ws.getbyid(params['id']).subscribe(
prod=> {
this.prod= prod;
this.editprodForm.controls['prodnr'].setValue(prod.prodnr);
this.editprodForm.controls['proddesc'].setValue(prod.proddesc);
}
);
}
);
}
onupdatprod() {
this.areWeWaiting = true;
let updatprod= new Prod(this.editprodForm.value);
updatprod.prod_id= this.prod.prod_id;
this.ws.update(updatprod).subscribe(
result => {
if (result === true) {
Materialize.toast('success', 4000);
} else {
}
},
error => {
}
);
}
I have prepared the following testing code :
it('should call service.Updatewhen onupdateprod, Validation Error', done => {
const mock = [];
spyOn(component['as'], 'UpdateAlarms').and.callThrough()
let updateprod = new Prod('1231');
updateprod.prodnr= '4';
updateprod.proddesc= 'UpdateTest';
updateprod.prod_id= '1';
component['ws'].update(updateprod ).subscribe(fail => {
console.log('Validation Error')
console.log(fail)
expect(fail).toBeFalsy();
done();
});
component.onupdatprod();
})
The error message I am receiving is:
TypeError: Cannot read property 'prod_id' of undefined
This error could be due to the fact that I'm trying to access `prod_id` which I get from `getbyid`, however, in the function `onupdatprod()` I attempt to set it using `updatprod.prod_id= this.prod.prod_id;`
If you have any suggestions or ideas on how to properly read the value of `prod_id`, please let me know.
Thanks for your help!