unit testing a promise method that implements .toPromise() on an http call

220 views
Skip to first unread message

Kiflemariam Andom

unread,
Feb 29, 2020, 4:59:38 AM2/29/20
to Angular and AngularJS discussion
My method is simply like this in angular 5:

  public login(data): Promise<any> {
   
const vm : any = this;

   
return new Promise((resolve, reject) => {
      let url
="www.example.com"
      vm
.http.post(url, data).toPromise().then(
        resp
=> {
          resolve
(resp);
       
}).catch(err=>{
          reject
(err)
       
})
   
});
 
}

The URL is usually dependent on the body but let's just assme it is example.com for now. I want to write a unit test that ensures an http call was made to a given url but for now, it just says "found none" and fails:


it
("should login user", fakeAsync(() => {

    let response
= {
      resultCount
: 1    
   
};

    service
   
.login(
     
{'name':'name'}
   
)
   
.then(result => {
      expect
(result).toEqual(response);
   
});

    flushMicrotasks
();


   
// Expect a call to this URL
     
const req = httpTestingController.expectOne(
     
"www.example.com"
   
);
    expect
(req.request.method).toEqual("POST");
    req
.flush(response);
    tick
();

 
}));

How do i unit test the method. It is driving me crazy.
Reply all
Reply to author
Forward
0 new messages