javascript - 如何在 mocha 中测试替换方法?
问题描述
我对 mocha 很陌生,并且一直在测试以下功能。我有以下字符串replace_underscore_with_hyphen
。我正在replace-underscore-with-hyphen
使用以下功能替换它。
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('-');
});
推荐阅读
- azure-powershell - Azure Powershell Runbook converting excel file in blob storage to csv
- azure - In the azure build pipeline, how to cleanup the docker images (created as part of the pipeline) from self hosted agent once it is pushed?
- reactjs - 用间隔反应 ApexCharts 更新图表
- javascript - 在打字稿中迭代嵌套的 JSON 字符串
- ajax - MVC:通过 Ajax 处理身份验证超时
- android - Makefile错误当我想为android构建项目时
- python - 将 Ipython 变量作为字符串参数传递给 shell 命令
- javascript - 在本地主机上进行区块链测试
- python - Pandas ValueError:只能将大小为 1 的数组转换为 Python 标量
- angular - Angular:绑定来自 Pubnub 的消息