lundi 27 avril 2015

How to write a Jasmine custom matcher

I am trying to write a Custom Matcher that tests if a string contains a special number of occurrences of a different string. This is what I did.

var matcher = {
    toContainTimes: function (expected, num) {
        return {
            compare: function(actual){
                actual.match(new RegExp(expected, "g") || []).length == num;
            }
        }
    }
}

But I get an error when executing this:

TypeError: Cannot read property 'pass' of undefined

My test looks like this:

expect(queryString).toContainTimes("&", 2);

It should return true if the string "&" occurs exactly twice in queryString. What did I do wrong?

Aucun commentaire:

Enregistrer un commentaire