Last active
December 20, 2015 21:19
-
-
Save gr2m/6196388 to your computer and use it in GitHub Desktop.
sinon expect helpers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// adds expect.js helpers for promises: | |
// | |
// expect(promise).to.be.promise() | |
// expect(promise).to.be.resolved() | |
// expect(promise).to.be.resolvedWith(['love']) | |
// expect(promise).to.be.rejected() | |
// expect(promise).to.be.rejectedWith(['hate']) | |
// expect(promise).to.be.pending() | |
expect.Assertion.prototype.promise = function () { | |
var isPromise = (typeof this.obj.done === 'function' && this.obj.resolve === undefined); | |
this.assert( | |
isPromise | |
, function(){ return 'expected promise'}); | |
return this; | |
}; | |
expect.Assertion.prototype.resolved = function () { | |
this.assert( | |
expect.eql(this.obj.state(), 'resolved') | |
, function(){ return 'expected to be resolved'}); | |
return this; | |
}; | |
expect.Assertion.prototype.resolvedWith = function (obj) { | |
var resolvedWith; | |
this.obj.done( function() { resolvedWith = Array.prototype.slice.call(arguments) }); | |
this.assert( | |
expect.eql(obj, resolvedWith) | |
, function(){ return 'expected to resolve with ' + JSON.stringify(obj) + ', was: ' + JSON.stringify(resolvedWith)} | |
, function(){ return 'expected to not resolve with ' + JSON.stringify(obj) + ', was: ' + JSON.stringify(resolvedWith)}); | |
return this; | |
}; | |
expect.Assertion.prototype.rejected = function () { | |
this.assert( | |
expect.eql(this.obj.state(), 'rejected') | |
, function(){ return 'expected to be resolved'}); | |
return this; | |
}; | |
expect.Assertion.prototype.rejectedWith = function (obj) { | |
var rejectedWith; | |
this.obj.fail( function() { rejectedWith = Array.prototype.slice.call(arguments) }); | |
this.assert( | |
expect.eql(obj, rejectedWith) | |
, function(){ return 'expected to rejected with ' + JSON.stringify(obj) + ', was: ' + JSON.stringify(rejectedWith)} | |
, function(){ return 'expected to not rejected with ' + JSON.stringify(obj) + ', was: ' + JSON.stringify(rejectedWith)}); | |
return this; | |
}; | |
expect.Assertion.prototype.pending = function () { | |
this.assert( | |
expect.eql(this.obj.state(), 'pending') | |
, function(){ return 'expected to be resolved'}); | |
return this; | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// expect(method).to.be.calledWith('some', 'funk') | |
expect.Assertion.prototype.calledWith = function() { | |
var args = Array.prototype.slice.call(arguments); | |
var hit = false | |
for (var i = 0; i < this.obj.args.length; i++) { | |
if (expect.eql(this.obj.args[i], args)) { | |
hit = true | |
} | |
}; | |
this.assert( | |
hit | |
, function(){ return 'expected to be called with ' + JSON.stringify(args) + ', calls where: ' + JSON.stringify(this.obj.args)} | |
, function(){ return 'expected to not be called with ' + JSON.stringify(args) }); | |
return this | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment