Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在摩卡测试替换方法?_Javascript_Node.js_Mocha.js_Chai - Fatal编程技术网

Javascript 如何在摩卡测试替换方法?

Javascript 如何在摩卡测试替换方法?,javascript,node.js,mocha.js,chai,Javascript,Node.js,Mocha.js,Chai,我对摩卡很陌生,一直在测试下面的功能。我有以下字符串用连字符替换下划线。我使用以下功能将其替换为将下划线替换为连字符 const type = "replace_underscore_with_hyphen"; type = type.replace(/_/ig, '-'); 但请允许我知道如何在mocha中测试此功能 您可以测试最后一个字符串是否包含连字符,而不是下划线: const replaceUnderscores = () => { const type = "r

我对摩卡很陌生,一直在测试下面的功能。我有以下字符串
用连字符替换下划线。我使用以下功能将其替换为
将下划线替换为连字符

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

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

您可以测试最后一个字符串是否包含连字符,而不是下划线:

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

这取决于这段代码发生的位置。这个问题没有包含它的上下文。