Is there a way to have a custom error on a custom matcher?

12 views
Skip to first unread message

Jeremy Lightsmith

unread,
Aug 27, 2010, 4:22:30 PM8/27/10
to Jasmine
I'm trying to write a "toHaveClass" matcher, and it'd be nice if the
error the user saw was, "I expected a tag to have class (foo) but it
only had classes (bar, baz, and ralph)"

Can I do that in Jasmine?

Jeremy Lightsmith

unread,
Aug 27, 2010, 4:49:45 PM8/27/10
to Jasmine
Putting this in SpecHelper.js actually seems to do the trick.  Should this be in the docs somewhere?

beforeEach(function () {
    this.addMatchers({
        toHaveClass: function (expectedClass) {
            var node = this.actual;
            this.message = function (expectedClass) { 
                return [
                    "expected tag to have class (" + expectedClass + "), but it only had classes (" + this.actual.attr('class') + ")",
                    "expected tag not to have class (" + expectedClass + "), but it did",
                ]; }
            return node.hasClass(expectedClass);
        }
    })
});

----
Jeremy Lightsmith
Helping Software Teams Get More Productive with tools like Agile, Lean, & Rails
312-953-1193
http://jeremylightsmith.com/
http://facilitationpatterns.org/ (book in progress)
http://www.linkedin.com/in/jeremylightsmith




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


Christian Williams

unread,
Aug 27, 2010, 5:05:27 PM8/27/10
to jasmi...@googlegroups.com
Yep, that's how you do it. It should definitely be in the docs, haven't gotten around to it yet, mea culpa.

I don't think you want to have "expectedClass" in the arg list for the message function though...?

--Xian
Reply all
Reply to author
Forward
0 new messages