首页 > 解决方案 > 如何在 mocha 中测试替换方法?

问题描述

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

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

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

标签: javascriptnode.jsmocha.jschai

解决方案


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

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('-');
});

推荐阅读