The below is AbcService.ts file,
I want to test this service file whether it is retuning specified request/data
It is calling 1 time as expected but rejecting the promise not returning anything
the error i'm getting is HttpClient.post received a call with arguments but all
configured strategies specify other arguments.
please help?
How to write test cases in jasmine for above code?
@injectable({
provideIn:'root'
})
export class AbcService{
constructor(private client:HttpClient){}
async imbook(json: string): Promise<any>{
let search!: SearchDefinition;
search = Object.assign(JSON.parse(json));
const httpOptions={
headers: new HttpHeaders({
'content-Type':'application/json',
'Access-Control-Allow-Origin':'*'
})
};
const req =
this.client.post("https://abc.zone/api/data",search,httpOptions).pipe(timeout(360000))
.toPromise()
await req;
return req;
}
}
here is test setup in spec file
describe('AbcService',()=>{
let service: AbcService;
let httpclientspy: jasmine.spyObj<HttpClient>;
let httpclientspyobj: jasmine.createSpyObj('HttpClient',['post']);
});
beforeEach(()=>TestBed.configureTestingModule({
imports: [HttpClientTestingModule],
providers:[AbcService,{
provide: HttpClient, useValue: httpclientspyobj
}]
}));
beforeEach(() =>{
service = TestBed.inject(AbcService);
client = TestBed.inject(HttpClient);
httpclientspy = TestBed.inject(HttpClient) as jasmine.SpyObj<HttpClient>
});
here is the testcase.
it('it should call http post method and return req',()=>{
const json= '{"id":"1","count":"5","name":"book name"}';
const httpOptions={
headers: new HttpHeaders({
'Content-Type':'application/json',
'Access-Control-Allow-Origin':'*'})
};
httpclient.post.withArgs('https://abc.zone/api/data',json,httpOptions).and.resolveTo();
service.imbook(json);
expect(httpclientspy.post).toHavebeenCalledTimes(1);
})
It is calling 1 time as expected but rejecting the promise not returning anything
the error i'm getting is HttpClient.post received a call with arguments but all
configured strategies specify other arguments.
please help?
How to write test cases in jasmine for above code?
You can try this:
describe('AbcService',()=>{
let service: AbcService;
let httpTestingController: HttpTestingController;
beforeEach(()=>TestBed.configureTestingModule({
imports: [HttpClientTestingModule],
providers:[AbcService]
}));
beforeEach(() =>{
service = TestBed.inject(AbcService);
client = TestBed.inject(HttpTestingController);
});
// !! add async here
it('it should call http post method and return req', async ()=>{
const json= '{"id":"1","count":"5","name":"book name"}';
const httpOptions={
headers: new HttpHeaders({
'Content-Type':'application/json',
'Access-Control-Allow-Origin':'*'})
};
// !! get a handle on the promise
const promise = service.imbook(json);
// !! get a handle on the http request in flight
const req = client.expectOne(request => request.method === 'POST');
// !! send this response to the http call
req.flush({});
// !! await the promise
const result = await promise;
// !! expect the result to be what we sent
expect(result).toEqual({});
});
To learn more about testing services with Http calls, refer to this link: https://testing-angular.com/testing-services/#testing-a-service-that-sends-http-requests.