在 Jasmine 1.3 中添加自定义匹配器 - 显示未定义
Add a custom matcher in Jasmine 1.3 - shows undefined
我在 jasmine 1.3 中使用量角器,尝试使用示例 here
向我的规范添加自定义匹配器
beforeEach(function () {
utils.log("beforeEach");
this.addMatchers({
toBeGoofy: function (expected) {
if (expected === undefined) {
expected = '';
}
var pass = this.actual.hyuk === "gawrsh" + expected;
if (pass) {
this.message = "Expected " + this.actual + " not to be quite so goofy";
} else {
this.message = "Expected " + this.actual + " to be goofy, but it was not very goofy";
}
return pass;
},
});
});
请注意,我没有对他们的示例进行任何更改。
在那之后,我尝试在 "it" 中使用它:
expect({ "hyuk": "j" }).toBeGoofy();
我得到一个错误:
TypeError: undefined is not a function
在匹配器使用的那一行..
有帮助吗?
问题显然出在匹配器定义上。
而不是:
if (pass) {
this.message = "Expected " + this.actual + " not to be quite so goofy";
} else {
this.message = "Expected " + this.actual + " to be goofy, but it was not very goofy";
}
此消息应该是 2 条消息的数组,第一个用于传递,第二个用于 not.matcher,所以它应该是这样的:
this.message = function() {
return [
"Expected " + this.actual.hyuk + " to be gawrsh",
"Expected " + this.actual.hyuk + " not to be gawrsh"
];
};
我在 jasmine 1.3 中使用量角器,尝试使用示例 here
向我的规范添加自定义匹配器 beforeEach(function () {
utils.log("beforeEach");
this.addMatchers({
toBeGoofy: function (expected) {
if (expected === undefined) {
expected = '';
}
var pass = this.actual.hyuk === "gawrsh" + expected;
if (pass) {
this.message = "Expected " + this.actual + " not to be quite so goofy";
} else {
this.message = "Expected " + this.actual + " to be goofy, but it was not very goofy";
}
return pass;
},
});
});
请注意,我没有对他们的示例进行任何更改。 在那之后,我尝试在 "it" 中使用它:
expect({ "hyuk": "j" }).toBeGoofy();
我得到一个错误:
TypeError: undefined is not a function
在匹配器使用的那一行.. 有帮助吗?
问题显然出在匹配器定义上。 而不是:
if (pass) {
this.message = "Expected " + this.actual + " not to be quite so goofy";
} else {
this.message = "Expected " + this.actual + " to be goofy, but it was not very goofy";
}
此消息应该是 2 条消息的数组,第一个用于传递,第二个用于 not.matcher,所以它应该是这样的:
this.message = function() {
return [
"Expected " + this.actual.hyuk + " to be gawrsh",
"Expected " + this.actual.hyuk + " not to be gawrsh"
];
};