I want to perform a custom assertion about the argument passed to a spied-on function call. Can I supply a callback to be used within the expectation against an argument?
expect(my.method).toHaveBeenCalledWith(jasmine.argumentMatching(myCustomCallback), jasmine.any({}));
... Where jasmine.argumentMatching(myCustomCallback)
is pseudo code.
Jasmine spy has a property .calls
, which provides information about the number of calls, arguments for particular calls, etc. Take a look at this docs section - Other tracking properties . It depends on your requirements which one would you use, and in documentation it is described very well, but in general:
.calls.argsFor(n)
- returns array of arguments for n-th call
.calls.allArgs()
- returns array of arrays of arguments for all calls
.calls.mostRecent()
, .calls.first()
- return an object in form:
{ object: my, args: ['foo', 42], returnValue: undefined }
.calls.all()
- returns array of objects (like one above)
In general it will look like:
spyOn(my, 'method');
my.method('foo', 42);
expect(my.method.calls.argsFor(0)).toEqual(['foo', 42]);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.