【问题标题】:Override default jasmine error message for matchers覆盖匹配器的默认茉莉花错误消息
【发布时间】:2013-06-03 04:52:02
【问题描述】:

我想在任何匹配器失败时显示一些自定义错误消息,假设我称之为

 expect(false).toBe(true);

显然它会返回 false 并且错误消息会是

Expected false to be true 

但在这里我想显示我的自定义消息。假设我想显示

You are expecting a false but its true .

如何做到这一点。提前致谢 。

【问题讨论】:

    标签: javascript unit-testing bdd jasmine


    【解决方案1】:

    这不是您想要的,因为它不灵活,但它可以处理您的情况。

    expect 中存在未记录的功能 - 您可以包含自定义失败消息:

    expect(false).toEqual(true, 'this is my custom error message');
    

    在你的情况下:

    expect(false).toBe(true, 'You are expecting a false but its true .');
    

    否则,创建一个自定义匹配器 (http://jasmine.github.io/2.0/custom_matcher.html):

    jasmine.addMatchers({
        customToBe: function() {
            return {
                compare: function(actual, expected) {
                    var result = {};
                    result.pass = actual === expected;
                    if (result.pass) {
                        result.message = "You are expecting a " + expected + " and its " + actual + " .";
                    } else {
                        result.message = "You are expecting a " + expected + " but its " + actual + " .";
                    }
                }
            };
        }
    });
    

    【讨论】:

      【解决方案2】:

      您必须为此编写自定义匹配器。因为错误消息是用茉莉花硬编码的。

      【讨论】:

        猜你喜欢
        • 2013-02-19
        • 2011-02-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-01
        • 2019-03-20
        • 2017-01-15
        相关资源
        最近更新 更多