Merge pull request #19 from logicalparadox/respond-to

Assertion#respondTo
This commit is contained in:
Jake Luer 2012-02-24 12:01:00 -08:00
commit 973f8cf517
4 changed files with 93 additions and 0 deletions

26
chai.js
View file

@ -908,6 +908,32 @@ Assertion.prototype.throw = function (constructor) {
return this; 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. * Aliases.
*/ */

View file

@ -855,6 +855,32 @@ Assertion.prototype.throw = function (constructor) {
return this; 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. * Aliases.
*/ */

View file

@ -427,4 +427,24 @@ suite('expect', function () {
}, "expected [Function] to throw error matching /hello/ but got \'testing\'"); }, "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\'");
});
}); });

View file

@ -419,4 +419,25 @@ suite('should', function() {
(badFn).should.throw(/hello/); (badFn).should.throw(/hello/);
}, "expected [Function] to throw error matching /hello/ but got \'testing\'"); }, "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\'");
});
}); });