Skip to content

Instantly share code, notes, and snippets.

@gr2m
Created March 25, 2012 06:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save gr2m/2191748 to your computer and use it in GitHub Desktop.
Save gr2m/2191748 to your computer and use it in GitHub Desktop.
Jasmine Promise Matchers
jasmine.Matchers.prototype.toBePromise = ->
this.actual.done && !this.actual.resolve
jasmine.Matchers.prototype.toBeRejected = -> this.actual.isRejected()
jasmine.Matchers.prototype.toBeResolved = -> this.actual.isResolved()
jasmine.Matchers.prototype.toBeResolvedWith = ->
expectedArgs = jasmine.util.argsToArray(arguments);
unless this.actual.done
throw new Error('Expected a promise, but got ' + jasmine.pp(this.actual) + '.');
done = jasmine.createSpy 'done'
this.actual.done done
this.message = ->
if done.callCount == 0
return [
"Expected spy " + done.identity + " to have been resolved with " + jasmine.pp(expectedArgs) + " but it was never resolved.",
"Expected spy " + done.identity + " not to have been resolved with " + jasmine.pp(expectedArgs) + " but it was."
];
else
return [
"Expected spy " + done.identity + " to have been resolved with " + jasmine.pp(expectedArgs) + " but was resolved with " + jasmine.pp(done.argsForCall),
"Expected spy " + done.identity + " not to have been resolved with " + jasmine.pp(expectedArgs) + " but was resolved with " + jasmine.pp(done.argsForCall)
];
return this.env.contains_(done.argsForCall, expectedArgs);
jasmine.Matchers.prototype.toBeRejectedWith = ->
expectedArgs = jasmine.util.argsToArray(arguments);
unless this.actual.fail
throw new Error('Expected a promise, but got ' + jasmine.pp(this.actual) + '.');
fail = jasmine.createSpy 'fail'
this.actual.fail fail
this.message = ->
if fail.callCount == 0
return [
"Expected spy " + fail.identity + " to have been resolved with " + jasmine.pp(expectedArgs) + " but it was never resolved.",
"Expected spy " + fail.identity + " not to have been resolved with " + jasmine.pp(expectedArgs) + " but it was."
];
else
return [
"Expected spy " + fail.identity + " to have been resolved with " + jasmine.pp(expectedArgs) + " but was resolved with " + jasmine.pp(fail.argsForCall),
"Expected spy " + fail.identity + " not to have been resolved with " + jasmine.pp(expectedArgs) + " but was resolved with " + jasmine.pp(fail.argsForCall)
];
return this.env.contains_(fail.argsForCall, expectedArgs);
jasmine.Matchers.prototype.toBePromise = function() {
return this.actual.done && !this.actual.resolve;
};
jasmine.Matchers.prototype.toBeRejected = function() {
return this.actual.isRejected();
};
jasmine.Matchers.prototype.toBeResolved = function() {
return this.actual.isResolved();
};
jasmine.Matchers.prototype.toBeResolvedWith = function() {
var done, expectedArgs;
expectedArgs = jasmine.util.argsToArray(arguments);
if (!this.actual.done) {
throw new Error('Expected a promise, but got ' + jasmine.pp(this.actual) + '.');
}
done = jasmine.createSpy('done');
this.actual.done(done);
this.message = function() {
if (done.callCount === 0) {
return ["Expected spy " + done.identity + " to have been resolved with " + jasmine.pp(expectedArgs) + " but it was never resolved.", "Expected spy " + done.identity + " not to have been resolved with " + jasmine.pp(expectedArgs) + " but it was."];
} else {
return ["Expected spy " + done.identity + " to have been resolved with " + jasmine.pp(expectedArgs) + " but was resolved with " + jasmine.pp(done.argsForCall), "Expected spy " + done.identity + " not to have been resolved with " + jasmine.pp(expectedArgs) + " but was resolved with " + jasmine.pp(done.argsForCall)];
}
};
return this.env.contains_(done.argsForCall, expectedArgs);
};
jasmine.Matchers.prototype.toBeRejectedWith = function() {
var expectedArgs, fail;
expectedArgs = jasmine.util.argsToArray(arguments);
if (!this.actual.fail) {
throw new Error('Expected a promise, but got ' + jasmine.pp(this.actual) + '.');
}
fail = jasmine.createSpy('fail');
this.actual.fail(fail);
this.message = function() {
if (fail.callCount === 0) {
return ["Expected spy " + fail.identity + " to have been resolved with " + jasmine.pp(expectedArgs) + " but it was never resolved.", "Expected spy " + fail.identity + " not to have been resolved with " + jasmine.pp(expectedArgs) + " but it was."];
} else {
return ["Expected spy " + fail.identity + " to have been resolved with " + jasmine.pp(expectedArgs) + " but was resolved with " + jasmine.pp(fail.argsForCall), "Expected spy " + fail.identity + " not to have been resolved with " + jasmine.pp(expectedArgs) + " but was resolved with " + jasmine.pp(fail.argsForCall)];
}
};
return this.env.contains_(fail.argsForCall, expectedArgs);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment