Noah Easterly
unread,Mar 7, 2012, 4:33:21 PM3/7/12Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to Jasmine
I've got some methods that are passing around big honking objects
(BHOs), and since specific codepaths only affect parts of the BHO,
it's a bit of a pain in the tuckus to specify the entire BHO every
time I want to do a `expect( ... ).toEqual( ... )` or a
`expect( ... ).toHaveBeenCalledWith( ... )`. It's line-noise in my
tests, and means I have to change unrelated tests when some
functionality changes.
So, with that in mind, I stuck `jasmine.contains()` in my
`SpecHelpers.js` file, and I'd thought I'd post it here if it might be
useful to other folks (or if you all wanted to school me on my code).
`jasmine.contains()` lets you be a bit more fine grained than
`jasmine.any(Object)` without having to specify all the pairs in the
object.
jasmine.contains = function(expectedPairs){
return new jasmine.Matchers.Contains(expectedPairs);
};
jasmine.Matchers.Contains = function(expectedPairs){
this.expectedPairs = expectedPairs;
this.env = jasmine.getEnv();
};
// subclass jasmine.Matchers.Any so that env.equals_ calls our
custom matches method
jasmine.Matchers.Contains.prototype = jasmine.any(Object);
jasmine.Matchers.Contains.prototype.toString = function(){
return '<jasmine.contains(' + jasmine.pp( this.expectedPairs ) +
')>';
};
jasmine.Matchers.Contains.prototype.classMatches =
jasmine.Matchers.Contains.prototype.matches;
jasmine.Matchers.Contains.prototype.matches = function(other){
if (!this.classMatches(other))
return false;
for (var property in this.expectedPairs)
if (!this.env.equals_(this.expectedPairs[property],
other[property])
return false;
return true;
};
So that's not a lot of code, but it's pretty handy:
expect( foo.bar ).toHaveBeenCalledWith( 'baz',
jasmine.contains({ quux: jasmine.Any(Number) }) );
If there's interest, I could always send it as a pull request to the
github project and see what the devs think.