adding custom Matchers in Jasmine 2.1.3

137 views
Skip to first unread message

Nathan Sinclair

unread,
Jan 21, 2015, 4:37:57 PM1/21/15
to jasmi...@googlegroups.com
Hello,

I've been searching the group for some advice about adding custom matchers in Jasmine 2.1.3, and because i guess the problem may be specific to my situation, here's what i've done.

this is the matcher;

this.addMatchers({
            toBeAGoodInvestment = function () {
                var investment = this.actual;
                return investment.isGood();
            };
        });

for clarification isGood() is a prototype function of the Investment class. it returns good if an investment increases in stockPrice.

the expectation is written like this

expect(investment).toBeAGoodInvestment();

I tried to add the Matcher in the SpecHelper.js file, when I tried running the SpecHelper page, it through up an error saying

expect(...).toBePlaying is not a function in file:///H:/njs236/Desktop/GitHub/rocketreading/spec/PlayerSpec.js (entirely unrelated to the function that I'm trying to implement.

So I removed it from there, and the other Matcher ran as it should.

Then I put it in a beforeEach = function () in the actual spec file. Whenever I do this it removes the set of tests run from SpecHelper.html

I looked at the advice of someone from the group, to put a reference to the Custom Matcher in the spec, but it doesn't show up when you run the page.

If I disable the toBePlaying custom Matcher in SpecHelper.js and don't have the toBeAGoodInvestment function, when I run the page it says

expect(...).toBeAGoodInvestment is not a function in file:///H:/njs236/Desktop/GitHub/rocketreading/spec/InvestmentSpec.js

obviously this is the only situation where any seem to work together (but the tests fail)

if I put it in the InvestmentSpec.js file, it won't display any tests that are given in this file.

Looking forward to hearing your thoughts.

Nathan

Gregg Van Hove

unread,
Jan 23, 2015, 4:44:31 PM1/23/15
to jasmi...@googlegroups.com
The matcher code you've given looks like it is for jasmine 1.3.x. The syntax for adding matchers changed in 2.0. The docs for upgrading are here: http://jasmine.github.io/2.0/upgrading.html

Hope that helps.

-Gregg

--
You received this message because you are subscribed to the Google Groups "Jasmine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jasmine-js+...@googlegroups.com.
To post to this group, send email to jasmi...@googlegroups.com.
Visit this group at http://groups.google.com/group/jasmine-js.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages