我正在开发一个角度(2.4.0)/打字稿应用程序,它使用自定义货币管道,内部使用angular的内置CurrencyPipe格式化“ en-CA ”的输入货币字符串和' fr-CA '加拿大语言环境。在为法语案例编写单元测试时,对于期望给定有效输入字符串的格式化输出的快乐路径情况,
describe('for French locale', () => {
// get the mock custom currency pipe instance for 'fr-CA' locale as 'currencyPipeForFR'
it('should be formatted for fr-CA locale', () => {
expect(currencyPipeForFR.transform('7500')).toBe('7 500 $');
});
});
我收到此错误,
Expected '7 500 $' to be '7 500 $'.
我确实检查了转化结果的 实例,它是String
。我错过了什么?任何帮助将不胜感激。
答案 0 :(得分:3)
嗯,罪魁祸首是角度内置的 CurrencyPipe 用于' fr-CA的分组/千位分隔符 '语言环境。在检查字符串中每个字符的UTF-16代码单元值时,我能够在索引 1 处看到分组/千位分隔符字符(\ u00A0)(在 7 和 5 之间,管道的输出值' 7 500 $ ')与正常空格键字符(\ u0020)。 ' $ '之前的空间签署预期价值' 7 500 $ '相当于\ u0020(正常空格键字符),因为它是在自定义管道逻辑中手动附加到内置管道的格式化结果。
因此,作为使用依赖于区域设置的管道(CurrrencyPipe,DecimalPipe)的此类实例(我的用例并非真正需要)的通用解决方案,我能够通过制作单元测试来正确检查预期值使用此#!/bin/bash
arr=(pdf doc txt) ## dynamically built array of extensions
n=${#arr[@]} ## number of elements in array
regex='^.*[.]\(' ## beginning of regex
srch="${1:-.}" ## path to search (default '.')
for ((i = 0; i < $n; i++)); do ## loop over each element
## if not last, add "${arr[i]}\|" otherwise add "${arr[i]}\)"
((i < n - 1)) && regex="$regex${arr[i]}\|" || regex="$regex${arr[i]}\)"
done
regex="$regex\$" ## add the final '$'
find "$srch" -type f -regex "$regex" ## execute the find
属性的toLocaleString()
方法,
Number.prototype