使用自定义匹配器的jasmine 2.0测试失败:undefined不是函数

时间:2014-06-02 02:43:33

标签: javascript jasmine

我在源文件中有这个功能:

function gimmeANumber(){
    var x = 4;
    return x;
}

this tutorial

借来的规范
describe('Hello world', function() {

    beforeEach(function() {
        this.addMatchers({
            toBeDivisibleByTwo: function() {
                return (this.actual % 2) === 0;
            }
        });
    });

    it('is divisible by 2', function() {
        expect(gimmeANumber()).toBeDivisibleByTwo();
    });

});

这是错误:

TypeError:undefined不是函数     在对象。 (文件:///家/ N /富/茉莉/茉莉-2.0.0 /距离/规格/ HelloWorldSpec.js ...) 谢谢。

1 个答案:

答案 0 :(得分:37)

自1.3以来,添加自定义匹配器的API已发生变化。 您可以看到更改here

Here现在是如何运作的:

function gimmeANumber() {
    var x = 4;
    return x;
}

describe('Hello world', function () {

    beforeEach(function () {
        jasmine.addMatchers({
            toBeDivisibleByTwo: function () {
                return {
                    compare: function (actual, expected) {
                        return {
                            pass: (actual % 2) === 0
                        };
                    }
                };
            }
        });
    });

    it('is divisible by 2', function () {
        expect(gimmeANumber()).toBeDivisibleByTwo();
        expect(5).not.toBeDivisibleByTwo();
    });

});