7

I can't seem to find a solution online.

Here is a sample of code so you get the issue :

// Spy on the wanted function
spyOn(object, 'myFunction');

// Call it 3 times with different parameters
object.myFunction('');
object.myFunction('', 0);
object.myFunction('', 0, true);

// Now all of these expects work
expect(object.myFunction).toHaveBeenCalledTimes(3);
expect(object.myFunction).toHaveBeenCalledWith('', 0);
expect(object.myFunction).toHaveBeenCalledWith('');
expect(object.myFunction).toHaveBeenCalledWith('', 0, true);

I would like to test if every call was correctly made. Is there a way to say something like this ?

expect(object.myFunction).nthCall(2).toHaveBeenCalledWith('', 0, true);

???

1 Answers1

8

There's calls property, you can use like: expect(object.myFunction.calls.argsFor(2)).toEqual(['', 0, true])

k102
  • 7,861
  • 7
  • 49
  • 69
  • 1
    I don't have access to the `calls` property, maybe `call` ? –  Jan 11 '18 at 15:16
  • @trichetriche sorry, I've made a mistake. now edited – k102 Jan 11 '18 at 15:17
  • @trichetriche that's wired, you should: https://jasmine.github.io/2.0/introduction.html#section-Other_tracking_properties are you sure, that `object.myFunction` is a spy? – k102 Jan 11 '18 at 15:20
  • Oh wait, I forgot to mention I use Typescript, could it be that my definitions aren't up-to-date ? (the project is about 1 month old) –  Jan 11 '18 at 15:24
  • And object.myFunction isn't a spy, it's a function that I spy on. I don't know if this is what you meant ? –  Jan 11 '18 at 15:25
  • It's a spy, in terms of jasmine. Well... typescript may be an issue, but `calls` is older than one month – k102 Jan 11 '18 at 15:28
  • 2
    Oh wait : In my IDE, the function was still typed as a class. I used `(object.myFunction as jasmine.Spy)`, and it gave me calls ! thank you for your help :) –  Jan 11 '18 at 15:34