我的源文件中有这个函数:
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();
});
});
这是错误:
类型错误:未定义不是函数 在对象。 (文件:///home/n/foo/jasmine/jasmine-2.0.0/dist/spec/HelloWorldSpec.js ...) 谢谢。
最佳答案
自 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();
});
});
关于javascript - 使用自定义匹配器的 jasmine 2.0 测试失败 : undefined is not a function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23986665/