ObjectContaining matcher and jasmine pull request #100

70 views
Skip to first unread message

Gregg Van Hove

unread,
Jun 20, 2011, 6:14:02 PM6/20/11
to jasmine...@googlegroups.com
After working with RSpec for a bit, I wanted to be able to use something like the hash_containing method with my expectations in jasmine. So that instead of:

expect(foo).toHaveBeenCalled();
var objectArg = foo.mostRecentCall.args[0];
expect(objectArg.bar).toEqual('baz');

I could just write:

expect(foo).toHaveBeenCalledWith(jasmine.objectContaining({bar: 'baz'}));

As I was writing this I tried to model it on the existing jasmine.Any object. I ended up encountering a couple of places in the code where there were explicit checks like
if (a instanceof jasmine.Matchers.Any) {
return a.matches(b);
}
or
if (value instanceof jasmine.Matchers.Any) {
this.emitScalar(value.toString());
}

It seemed like it would be bad form to tack in a new instanceof check. I ended up changing both equals_ and PrettyPrinter.format to check for the existance of a specific method on the object rather than it's type. I called the methods jasmineMatches and jasmineToString so they could be mixed in only in tests and not conflict with any existing methods already named matches or toString on the objects. I talked through this a bit with Davis today, but I wanted to get out a description of the idea behind the pull request in case there were any other questions.

-Gregg

Davis Frank

unread,
Jun 20, 2011, 10:55:33 PM6/20/11
to jasmine...@googlegroups.com
I walked through this code with Gregg today & I think I understand the implementation and the refactoring opportunities he found & made.

It's worth some discussion before we pull it, so please review the diffs and let's talk a bit about them.

--dwf




--
You received this message because you are subscribed to the Google Groups "Jasmine Dev" group.
To post to this group, send email to jasmine...@googlegroups.com.
To unsubscribe from this group, send email to jasmine-js-de...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/jasmine-js-dev?hl=en.

Reply all
Reply to author
Forward
0 new messages