I would like to test an HTTP request in Angular 5, but it is not working. Here is the error message:
TypeError: newAlarms.generateUrlencodedParameters is not a function
and here is the code: It is a post request, and it working properly.
public createAlarms(newAlarms: Alarm): Observable<boolean> {
let headers = new Headers();
headers.append('Content-Type', 'application/x-www-form-urlencoded');
let body = newAlarms.generateUrlencodedParameters(this.getCurrentUser().token);
return this.http.post(Api.getUrl(Api.URLS.createAlarms), body, {
headers: headers
})
.map((response: Response) => {
let res = response.json();
if (res.StatusCode === 0) {
return true;
}
}
and here is the test for it: This is the test for the post request. I used different articles to write it and perhaps that is why it is not working.
describe('postAlarms()', () => {
it('should create alarm',
inject([AlarmsService, XHRBackend], (AlarmsService, MockBackend) => {
const loginDetails = {
alarmtype_id: 'gdfgr',
alarmdesc: 'retst',
alarmnumber: '521'
};
MockBackend.connections.subscribe((connection) => {
connection.mockRespond(new Response(new ResponseOptions({
})));
});
AlarmsService.createAlarms(loginDetails).subscribe((dd) => {
expect(dd.status).toEqual('ok');
});
}));
});
and this is my class:
export class Alarm {
alarmtype_id: number;
alarmnumber: String;
alarmdesc: string;
constructor(obj: any) {
this.alarmdesc = obj.alarmdesc;
this.alarmtype_id = obj.alarmtype_id;
this.alarmnumber = obj.alarmnumber;
}
public generateUrlencodedParameters(token: string, id?: number): string {
let urlSearchParams = new URLSearchParams();
urlSearchParams.append('alarmnumber', this.alarmnumber.toString());
urlSearchParams.append('alarmdesc', this.alarmdesc.toString());
urlSearchParams.append('token', token);
return urlSearchParams.toString();
}}
For this testing, I follow this post
Aucun commentaire:
Enregistrer un commentaire