mirror of
https://github.com/chaijs/chai
synced 2024-11-15 08:17:14 +00:00
Merge pull request #19 from logicalparadox/respond-to
Assertion#respondTo
This commit is contained in:
commit
973f8cf517
4 changed files with 93 additions and 0 deletions
26
chai.js
26
chai.js
|
@ -908,6 +908,32 @@ Assertion.prototype.throw = function (constructor) {
|
|||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* # .respondTo(method)
|
||||
*
|
||||
* Assert that object/class will respond to a method.
|
||||
*
|
||||
* expect(Klass).to.respondTo('bar');
|
||||
* expect(obj).to.respondTo('bar');
|
||||
*
|
||||
* @name respondTo
|
||||
* @param {String} method
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Assertion.prototype.respondTo = function (method) {
|
||||
var context = ('function' === typeof this.obj)
|
||||
? this.obj.prototype[method]
|
||||
: this.obj[method];
|
||||
|
||||
this.assert(
|
||||
'function' === typeof context
|
||||
, 'expected ' + this.inspect + ' to respond to ' + inspect(method)
|
||||
, 'expected ' + this.inspect + ' to respond to ' + inspect(method));
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
/*!
|
||||
* Aliases.
|
||||
*/
|
||||
|
|
|
@ -855,6 +855,32 @@ Assertion.prototype.throw = function (constructor) {
|
|||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* # .respondTo(method)
|
||||
*
|
||||
* Assert that object/class will respond to a method.
|
||||
*
|
||||
* expect(Klass).to.respondTo('bar');
|
||||
* expect(obj).to.respondTo('bar');
|
||||
*
|
||||
* @name respondTo
|
||||
* @param {String} method
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Assertion.prototype.respondTo = function (method) {
|
||||
var context = ('function' === typeof this.obj)
|
||||
? this.obj.prototype[method]
|
||||
: this.obj[method];
|
||||
|
||||
this.assert(
|
||||
'function' === typeof context
|
||||
, 'expected ' + this.inspect + ' to respond to ' + inspect(method)
|
||||
, 'expected ' + this.inspect + ' to respond to ' + inspect(method));
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
/*!
|
||||
* Aliases.
|
||||
*/
|
||||
|
|
|
@ -427,4 +427,24 @@ suite('expect', function () {
|
|||
}, "expected [Function] to throw error matching /hello/ but got \'testing\'");
|
||||
});
|
||||
|
||||
test('respondTo', function(){
|
||||
function Foo(){};
|
||||
Foo.prototype.bar = function(){};
|
||||
|
||||
var bar = {};
|
||||
bar.foo = function(){};
|
||||
|
||||
expect(Foo).to.respondTo('bar');
|
||||
expect(Foo).to.not.respondTo('foo');
|
||||
|
||||
expect(bar).to.respondTo('foo');
|
||||
|
||||
err(function(){
|
||||
expect(Foo).to.respondTo('baz');
|
||||
}, "expected [Function: Foo] to respond to \'baz\'");
|
||||
|
||||
err(function(){
|
||||
expect(bar).to.respondTo('baz');
|
||||
}, "expected { foo: [Function] } to respond to \'baz\'");
|
||||
});
|
||||
});
|
||||
|
|
|
@ -419,4 +419,25 @@ suite('should', function() {
|
|||
(badFn).should.throw(/hello/);
|
||||
}, "expected [Function] to throw error matching /hello/ but got \'testing\'");
|
||||
});
|
||||
|
||||
test('respondTo', function(){
|
||||
function Foo(){};
|
||||
Foo.prototype.bar = function(){};
|
||||
|
||||
var bar = {};
|
||||
bar.foo = function(){};
|
||||
|
||||
Foo.should.respondTo('bar');
|
||||
Foo.should.not.respondTo('foo');
|
||||
|
||||
bar.should.respondTo('foo');
|
||||
|
||||
err(function(){
|
||||
Foo.should.respondTo('baz');
|
||||
}, "expected [Function: Foo] to respond to \'baz\'");
|
||||
|
||||
err(function(){
|
||||
bar.should.respondTo('baz');
|
||||
}, "expected { foo: [Function] } to respond to \'baz\'");
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue