【问题标题】:Add a custom matcher in Jasmine 1.3 - shows undefined在 Jasmine 1.3 中添加自定义匹配器 - 显示未定义
【发布时间】:2015-09-24 13:41:16
【问题描述】:

我正在使用 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;
            },
        });
    });

请注意,我没有对他们的示例进行任何更改。 之后,我尝试在这样的“它”中使用它:

expect({ "hyuk": "j" }).toBeGoofy();

我得到一个错误:

TypeError: undefined is not a function

在使用匹配器的那一行.. 有什么帮助吗?

【问题讨论】:

  • 你把beforeEach()块放在哪里了?谢谢。
  • @alecxe 在几个地方尝试过,在第一个“it”之前,在第一个“describe”之前,规范的结构非常简单:一个描述中的 3 个“it”。那里的日志功能也可以按预期工作和打印...
  • 您确定您使用的是 jasmine v1.3 吗?如果您使用较新版本的 jasmine,通常会出现此错误。最新的是 v2.3。

标签: javascript testing jasmine protractor end-to-end


【解决方案1】:

问题显然是匹配器定义。 而不是:

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 个消息的数组,第一个是 pass,第二个是 not.matcher,所以它应该是这样的:

this.message = function() {
    return [
        "Expected " + this.actual.hyuk + " to be gawrsh",
        "Expected " + this.actual.hyuk + " not to be gawrsh"
    ];
};

【讨论】:

    猜你喜欢
    • 2017-10-02
    • 1970-01-01
    • 2020-07-07
    • 1970-01-01
    • 1970-01-01
    • 2019-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多