Created
May 7, 2014 21:53
-
-
Save jessehattabaugh/81ab7e4eafe0941b33a0 to your computer and use it in GitHub Desktop.
setsGets assertion helper for Chai
This file contains 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
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