Spec has no expectations - Jasmine is testing the callback function

I have a method that is called using d3 timer

. Whenever a method is called, the method emits an object with multiple values. One of the values โ€‹โ€‹increases over time. I would like to write a test to check if the values โ€‹โ€‹are in ascending order or not (i.e. increasing over time or not).

So, to solve this problem, in my test, I subscribe to the event emitter and inside the subscription, I push the object that I receive into a local array. And then I expect the value to array[i]

be less than array[i+1]

. I think my logic is perfectly correct, but I'm not sure why I am getting a message from Jasmine saying that the spec has no expectations

although I have one.

Here is the code:

let x = d3.timer((elapsed) => { 
    this.method(); // call the function
    if(elapsed >= 500) {
     x.stop(); // stops the timer.
    }
});

method(elapsed) {
 // do something
 if(elapsed > 500) {
   this.output.emit({x: somevalue, y: somevalue, f: increasingvalue });
 }
}

      

Jasmine Spectrum:

it('my spec', inject([JumpService], (service: JumpService) =>{
  array = [];
  //service calls the method
  service.output.subscribe(e => {
   array.push(e);
   // A console statement here will give me the length and the object pushed.
   for(let i = 0; i< array.length - 1; i++) {
    expect(array[i].f).toBeLessThan(array[i+1].f);
   }

  });

}));

      

Am I doing something wrong here? How can I solve such a scenario? Please point me in the right direction.

Thank.

+3


source to share


2 answers


In general, when testing asynchronous callback functions, it is always important to expect the test results to be resolved after promises have been resolved. You can use Angular's test layer framework tick()

with fakeAsync()

, or you can just drop the generic Jasmine method to test asynchronous methods withdone()

Usage done()

:

it('my spec', (done) => {
  array = [];
  service.output.subscribe(e => {
   array.push(e);
   for(let i = 0; i< array.length - 1; i++) {
    expect(array[i].f).toBeLessThan(array[i+1].f);
   }
   done();
  });
});

      



Hope this answer helps.

Note. I had no luck with fakeAsync()

and tick()

, therefore, I am not including it in the answer. Excuse me.

+2


source


Try using the function async

from @angular/core/testing

. it

Wraps a test function in an asynchronous test area. The test will automatically end when all asynchronous calls to this zone have been made. Can be used to wrap a {@link inject} call.



Sample code below:

it('...', async(inject([AClass], (object) => {
  object.doSomething.then(() => {
   expect(...);
  })
});

      

0


source







All Articles