0

I have just found about the Jasmine Framework, I am trying it out and though I should try writing a custom matcher, because it sounds extremely useful to be able to do. I did this:

describe('Hello World', function() {
    beforeEach(function() {
        this.addMatchers({
            toBeDivisbleByTwo: function() {
                var result = {
                    pass: (this.actual % 2) === 0
                };
                if(result.pass) {
                    result.message = 'this is divisible by two';
                } else {
                    result.message = 'this is not divisible by two';
                }

                return result;
            }
        });
    });
});

describe('Hello world', function() {
    it('divisible by two', function() {
        expect(evenNumberGenerator()).toBeDivisbleByTwo();
    });
});

but when I run the page I get this error in internet explorer:

TypeError: Object doesn't support property or method 'toBeDivisbleByTwo'

is this due to the order of loading or something?

1
  • I don't know Jasmine well, but: your beforeEach is in a different suite from where you're using toBeDivisibleByTwo. Is that a problem? If you move your beforeEach out of its containing suite into top-level runner code, does that fix the problem? Commented Jul 1, 2014 at 10:02

2 Answers 2

2

beforeEach functions only apply to the it functions within the describe which they are written and in any nested describe within that one. To solve your problem you can either nest your second describe inside your first or remove the second describe entirely and place your it inside the first one. It's up to you to decide how you want to organize your tests but in this case I recommend the second option because you have no it functions in the first describe as it is. Also, Jasmine syntax for describe and it is intended to read like plain English. So you can have something like...

describe('Even number generator', function() {
    beforeEach(function() {
        //Your matcher
    });

    it('should return a number that is divisible by two', function() {
        expect(evenNumberGenerator()).toBeDivisbleByTwo();
    });
});

This helps organize and make sense of your tests.

Sign up to request clarification or add additional context in comments.

Comments

1

You can also use Prolific library, that works along with Jasmine <2.0

https://github.com/Bitterbrown/prolific

(yes, I admit, I'm the developer :)) you can add your custom matcher, you'll find it on the readme file (I just updated it)

basically, after including prolific, you can do:

it("should be divisible by 2", function () {
  assume("var evenNumberGenerator() is divisible by 2");
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.