jasmine toHaveBeenCalledWith partial matching

JavascriptUnit TestingJasmine

Javascript Problem Overview


With Jasmine, I could spy on methods and figure out the arguments. I want to be able to call toHaveBeenCalledWith(something, anything).

Let's say I want to spy on a method .on(event, callback). All I care about is if the event is listened to rather than what the actual callback identity is. Is it possible to do this without writing a custom matcher? I don't see one.

Javascript Solutions


Solution 1 - Javascript

Try

toHaveBeenCalledWith(jasmine.any(Object), jasmine.any(Function))

Solution 2 - Javascript

Jasmine 2:

 expect(callback).toHaveBeenCalledWith(jasmine.objectContaining({
    bar: "baz"
  }));

https://jasmine.github.io/2.0/introduction.html

Solution 3 - Javascript

If you wish to test for specific things, you can do something like:

expect(mockSomething.someMethod.mostRecentCall.args[0].pool.maxSockets).toEqual(50);

The syntax in Jasmine 2 is now:

mockSomething.someMethod.calls.mostRecent().args[0]

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPwnnaView Question on Stackoverflow
Solution 1 - JavascriptbeautifulcoderView Answer on Stackoverflow
Solution 2 - JavascriptIevgen MartynovView Answer on Stackoverflow
Solution 3 - JavascriptCmagView Answer on Stackoverflow