I have reviewed Jasmine's documentation of the toHaveBeenCalledWith matcher in order to understand whether it's possible to pass in a regular expression for an argument, if that argument is expected to be a string. Unfortunately, this is unsupported functionality. There's also an issue open on github requesting this functionality.
I've dug a bit into the codebase, and I see how it might be possible to implement this inside the existing matcher. I think it would be more appropriate to implement it as a separate matcher though, so that the abstraction is captured individually.
In the meantime, what might be a good workaround?
Alternatively, if you are spying on a method on an object:
As jammon mentioned, the Jasmine 2.0 signature has changed. If you are spying on the method of an object in Jasmine 2.0, Nick's solution should be changed to use something like -
Sometimes it is more readable to write it this way:
It give more clear visibility of method arguments (instead of using array)
As of Jasmine 2.2, you can use
jasmine.stringMatching
:After doing some digging, I've discovered that Jasmine spy objects have a
calls
property, which in turn has a mostRecent() function. This function also has a child propertyargs
, which returns an array of call arguments.Thus, one may use the following sequence to perform a regexp match on call arguments, when one wants to check that the string arguments match a specific regular expression:
Pretty straightforward.
In Jasmine 2.0 the signature changed a bit. Here it would be:
And the Documentation for Jasmine 1.3 has moved.