dimanche 17 novembre 2019

SpyingOn multiple sync functions with Jasmine Angular7

I have an async function with calls two additional async functions that return data from a service. Then I check the status of what each async call returned and do some basic logic. I am trying to test by spying on each one and returning a value for each case.

export class service{

constructor (readony someOtherService: SomeOtherService){}

public async myfunction(): Promise<any> {
 const response1 = await this.getResponse1();
 const resposne2 = await this.getResponse2();
 let returnValue = 0l
if(response1){
    returnValue = 1
} else if (response2){
   returnValue = 2
}
return returnValue;


public async getResponse1() {
   this.someOtherService.checkFeature("isOneActive").toPromise();
}
public async getResponse2() {
   this.someOtherService.checkFeature("isTwoActive").toPromise();
}

}

I am trying to test like this

 describe(When my function is called, () =>{ 

     describe(And returnValue 1 is false AND returnValue2 is false, () => {

      beforeEach(function(){
       //works correctly, never enters getResponse1
       spyOn(service, 'getResponse1').andreturnValue(Promise.resolve(false))

       //does not appear to work, code always enters getResponse2, and then fails. 
       spyOn(service, 'getResponse2').andreturnValue(Promise.resolve(false)) 

       expect(service.myFunction()).toEqaul(0))
        }
    }
}

The interesting part is that the first call appears to be spied correctly and never enters 'getResponse1'. Now the second call ALWAYS enters 'getResponse2' even though it is spied in the same way. If I change the order, only the first 'getResponse' function is spied correctly.

From what I read it is completely possible to spyOn more than 1 async function in Jasmine.

Aucun commentaire:

Enregistrer un commentaire