mirror of
https://github.com/chaijs/chai
synced 2024-11-15 00:07:11 +00:00
Allow messages to be functions
This commit is contained in:
parent
ab999d8917
commit
216ad26aa1
4 changed files with 14 additions and 2 deletions
|
@ -88,8 +88,8 @@ module.exports = function (_chai, util) {
|
|||
*
|
||||
* @name assert
|
||||
* @param {Philosophical} expression to be tested
|
||||
* @param {String} message to display if fails
|
||||
* @param {String} negatedMessage to display if negated expression fails
|
||||
* @param {String or Function} message or function that returns message to display if fails
|
||||
* @param {String or Function} negatedMessage or function that returns negatedMessage to display if negated expression fails
|
||||
* @param {Mixed} expected value (remember to check for negation)
|
||||
* @param {Mixed} actual (optional) will default to `this.obj`
|
||||
* @api private
|
||||
|
|
|
@ -39,6 +39,7 @@ module.exports = function (obj, args) {
|
|||
, msg = negate ? args[2] : args[1]
|
||||
, flagMsg = flag(obj, 'message');
|
||||
|
||||
if(typeof msg === "function") msg = msg();
|
||||
msg = msg || '';
|
||||
msg = msg
|
||||
.replace(/#{this}/g, objDisplay(val))
|
||||
|
|
|
@ -8,6 +8,10 @@ describe('assert', function () {
|
|||
err(function () {
|
||||
assert(foo == 'baz', "expected foo to equal `bar`");
|
||||
}, "expected foo to equal `bar`");
|
||||
|
||||
err(function () {
|
||||
assert(foo == 'baz', function() { return "expected foo to equal `bar`"; });
|
||||
}, "expected foo to equal `bar`");
|
||||
});
|
||||
|
||||
it('fail', function () {
|
||||
|
|
|
@ -215,6 +215,13 @@ describe('utilities', function () {
|
|||
var obj = {};
|
||||
_.flag(obj, 'message', 'foo');
|
||||
expect(_.getMessage(obj, [])).to.contain('foo');
|
||||
|
||||
var obj = {};
|
||||
msg = function() { return "expected a to eql b"; }
|
||||
negateMsg = function() { return "expected a not to eql b"; }
|
||||
expect(_.getMessage(obj, [null, msg, negateMsg])).to.equal("expected a to eql b");
|
||||
_.flag(obj, 'negate', true);
|
||||
expect(_.getMessage(obj, [null, msg, negateMsg])).to.equal("expected a not to eql b");
|
||||
});
|
||||
});
|
||||
|
||||
|
|
Loading…
Reference in a new issue