Test a service with a promise in Angular with Karma-Jasmine


I am starting to test a service in Angularjs with Karma and Jasmine. I have no idea how $q works, i have never use it and anyway i do not know if it is necessary here. My problem is that I have a service which have 1 function which calls another function inside the service (with a promise):

This is the function which calls sendMetrics (which has the promise)

This is my test, i have tried with $q using $digest, but i get an error about cookies (I have search it and it could be because of my angular - jasmine versions). Also I have tried with jasmine spy, but i do not want to build again all the service.

I just want to resolve the promise to check that test value is 2 and not 1.

This is the function which has the promise.

Maybe this question is duplicated or not, but i need an answer for people who do not know how to use $q, if I need to use it. Until now, I have not found anything helpful.

I think that you have to return the promise directly in sendMetrics and then handle it in sendSearchMetrics. Right now, you are not returning a promise from within the promise in sendMetrics but you are trying to use it as if it were a Promise.