【问题标题】:Use custom function for validation Mocha/Chai使用自定义函数验证 Mocha/Chai
【发布时间】:2021-05-18 13:22:36
【问题描述】:

我刚开始使用 Chai/Mocha 进行测试。我有一个自定义函数,它使用正则表达式验证字符串(例如)。我想使用这个功能。这是我的代码:

describe('Custom Function', () => {
    it('Function 1', () => {
        // I'm calling a function here which returns a string, I want to validate that(only as example)
        expect(someFunction())
    });
});

这可能看起来很简单,但我是新手,很抱歉。现在,关于示例验证函数,我知道我可以使用.to.match(/SOME_REGEX/),但我在其他地方使用相同的函数,我不想重复代码。我的函数将从 API 获取数据并相应地验证测试结果

不与 - Mocha Chai custom comparison function 重复

【问题讨论】:

    标签: javascript testing mocha.js chai


    【解决方案1】:

    你可以使用 to.be.true 也可以使用 RegExp.test() 方法来举例:

    const regex = /^(H|HH)/;
    expect(regex.test(string)).to.be.true;
    

    【讨论】:

    • 正如我在问题中提到的,我有一个会生成字符串的函数。我希望该字符串由另一个自定义函数验证(假设该函数将数据发送到 API 并且 api 对其进行验证)。
    猜你喜欢
    • 1970-01-01
    • 2020-05-08
    • 1970-01-01
    • 2020-08-29
    • 2015-06-02
    • 1970-01-01
    • 1970-01-01
    • 2021-07-16
    • 2020-09-22
    相关资源
    最近更新 更多