Skip to content

Instantly share code, notes, and snippets.

@esbie
Created November 22, 2011 03:43
Show Gist options
  • Save esbie/1384835 to your computer and use it in GitHub Desktop.
Save esbie/1384835 to your computer and use it in GitHub Desktop.
Jasmine spy syntactic sugar for ignoring event arguments
/**
* Matcher that mimics toHaveBeenCalledWith, except it ignores the first argument (and isn't robust enough to handle more than one call)
*
* @example
*
*/
jasmine.Matchers.prototype.toHaveBeenTriggeredWith = function() {
var expectedArgs = jasmine.util.argsToArray(arguments);
if (!jasmine.isSpy(this.actual)) {
throw new Error('Expected a spy, but got ' + jasmine.pp(this.actual) + '.');
}
this.message = function() {
if (this.actual.callCount === 0) {
return [
"Expected spy " + this.actual.identity + " to have been called with " + jasmine.pp(expectedArgs) + " but it was never called.",
"Expected spy " + this.actual.identity + " not to have been called with " + jasmine.pp(expectedArgs) + " but it was."
];
} else {
return [
"Expected spy " + this.actual.identity + " to have been called with " + jasmine.pp(expectedArgs) + " but was called with " + jasmine.pp(this.actual.argsForCall[0].slice(1)),
"Expected spy " + this.actual.identity + " not to have been called with " + jasmine.pp(expectedArgs) + " but was called with " + jasmine.pp(this.actual.argsForCall[0].slice(1))
];
}
};
return this.actual.callCount > 0 && this.env.equals_(this.actual.argsForCall[0].slice(1), expectedArgs);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment