problem globally defining matchers

308 views
Skip to first unread message

fschwiet

unread,
Sep 15, 2010, 2:27:57 PM9/15/10
to Jasmine
I tried making some custom matchers globally available by extending
jasmine.Matchers.prototype, as described here:
http://testdrivenwebsites.com/2010/08/04/custom-jquery-matchers-in-jasmine/

What I'm finding is that my matchers always pass when I define them
globally this way, though they work as expected if I used
this.addMatchers() instead.

A repro is below. Two matchers are defined, which should always
fail. Only the second one will actually fail though. The failure
output indicates I'm running Jasmine rc1, I just grabbed the Jasmine
1.0 release and have the same issue.

jasmine.Matchers.prototype.isFailSample = function() {
return false;
};

describe("custom matchers", function() {

beforeEach(function() {
this.addMatchers({
isFailSampleInline: function() { return false; }
});
});

it("this test passes... it shouldnt", function() {

expect(1).isFailSample();
expect(1).not.isFailSample();
});

it("this test fails, as expected", function() {

expect(1).isFailSampleInline();
expect(1).not.isFailSampleInline();

});
});

test output:

custom matchers
runthis test passes... it shouldnt
runthis test fails, as expected
Expected 1 is fail sample inline.
Error("Expected 1 is fail sample inline.")@:0 ([object Object])@http://
canvas/js/lib/jasmine-1.0.0.rc1/jasmine.js:94 ()@http://canvas/js/lib/
jasmine-1.0.0.rc1/jasmine.js:1143 ()@http://canvas/js/TestUtilities/
CustomJasmineMatchers.tests.js:22 ((function () {if
(jasmine.Queue.LOOP_DONT_RECURSE && calledS

Christian Williams

unread,
Sep 15, 2010, 3:29:27 PM9/15/10
to jasmi...@googlegroups.com
Thanks for pointing out that blog post; the method he outlines there (extending jasmine.Matchers.prototype) is no longer supported. I'll contact the author.

The right way to handle this is to call this.addMatchers() from a global beforeEach() block:

beforeEach(function() {
  isFailSampleInline: function() { return false; }
});

In other words, simply move your beforeEach() block outside of any describe(). I like to create a SpecHelper.js file to hold stuff like that.

--Xian


--
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.


Reply all
Reply to author
Forward
0 new messages