【问题标题】:How do I test replace method in mocha?如何在 mocha 中测试替换方法?
【发布时间】:2018-10-02 13:41:56
【问题描述】:

我对 mocha 很陌生,并且一直在测试以下功能。我有以下字符串replace_underscore_with_hyphen。我正在使用以下功能将其替换为 replace-underscore-with-hyphen

const type = "replace_underscore_with_hyphen";
     type = type.replace(/_/ig, '-');

但是请我知道如何在 mocha 中测试此功能。

【问题讨论】:

  • 这取决于这段代码出现的位置。该问题不包含它的上下文。

标签: javascript node.js mocha.js chai


【解决方案1】:

您可以测试最终字符串是否包含连字符,并且不包含下划线:

const replaceUnderscores = () => {
  const type = "replace_underscore_with_hyphen";
  return type.replace(/_/ig, '-');
}

it('should replace underscores with hyphen', () => {
  const replaced = replaceUnderscores();
  expect(replaced).not.toContain('_');
  expect(replaced).toContain('-');
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-04
    • 2016-12-19
    • 1970-01-01
    • 2021-09-19
    • 1970-01-01
    • 2011-06-06
    • 2018-02-27
    相关资源
    最近更新 更多