Skip to content

Instantly share code, notes, and snippets.

@jessehattabaugh
Created May 7, 2014 21:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jessehattabaugh/81ab7e4eafe0941b33a0 to your computer and use it in GitHub Desktop.
Save jessehattabaugh/81ab7e4eafe0941b33a0 to your computer and use it in GitHub Desktop.
setsGets assertion helper for Chai
module.exports = function (chai, utils) {
var Assertion = chai.Assertion;
Assertion.addMethod('setsGets', function (val) {
var method = this._obj;
// setter/getters must be functions
new Assertion(method).to.be.a('function');
// should be empty at first
new Assertion(method()).to.not.exist;
// see if the function will accept
function setVal() {
method(val);
}
// use a different set of assertions if we're negated
if (utils.flag(this, 'negate')){
new Assertion(setVal).to.throw(Error);
new Assertion(method()).to.not.equal(val);
}
else {
new Assertion(setVal).to.not.throw(Error);
new Assertion(method()).to.equal(val);
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment