jasmine toHaveBeenCalledWith partial matching
37,453
Solution 1
Try
toHaveBeenCalledWith(jasmine.any(Object), jasmine.any(Function))
Solution 2
Jasmine 2:
expect(callback).toHaveBeenCalledWith(jasmine.objectContaining({
bar: "baz"
}));
https://jasmine.github.io/2.0/introduction.html
Solution 3
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]
Related videos on Youtube
Author by
Pwnna
Updated on October 09, 2020Comments
-
Pwnna over 3 years
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 theevent
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.-
Michael Freidgeim over 2 yearsSimilar question for jest is stackoverflow.com/questions/52337116/…
-
-
John McCarthy over 8 yearsUseful for additional optional arguments. Note the syntax in Jasmine 2 is now
mockSomething.someMethod.calls.mostRecent().args[0]
-
John Neuhaus about 6 yearsCan also care even less and use
jasmine.anything()
if the type doesn't matter -
oomer over 2 yearslet spy = spyOn(nameService, 'getName').and.returnValue(Observable.of('Ali')); nameService.getName('Henry'); expect(spy.calls.all()[0].args[0]).toEqual('Henry');