Jasmine 测试失败但 'expected' 和 'toBe' 字符串似乎相等?

Jasmine test fails but 'expected' and 'toBe' strings seem equal?

我正在开发一个使用自定义货币管道的 angular(2.4.0)/typescript 应用程序,它在内部使用 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。我错过了什么?任何帮助将不胜感激。

嗯,罪魁祸首是 angular 的内置 CurrencyPipe[=27= 使用的 grouping/thousand 分隔符 ] 对于“fr-CA”语言环境。在检查字符串中每个字符的 UTF-16 代码单元值时,我能够在索引 [=17= 处看到 grouping/thousand 分隔符 字符 (\u00A0) ]1(管道输出值的75之间 '7 500 $') 不同于正常的 space 条字符 (\u0020)。 space 在 '$' 符号之前的期望值 '7 500 $' 等同于 \u0020(正常 space 条字符),因为它是在自定义管道的逻辑中手动附加到内置管道的格式化结果中的。

因此,作为使用依赖于语言环境的管道(CurrrencyPipe、DecimalPipe)的此类实例(我的用例并不是真正需要的)的通用解决方案,我能够进行单元测试以正确检查预期值通过像这样使用 Number.prototype 属性 的 toLocaleString() 方法,

describe('for French locale', () => {
 // get the custom currency pipe instance for 'fr-CA' locale as 'currencyPipeForFR'
 const thousandSeparator = () => {
  return (1000).toLocaleString('fr-CA').substring(1, 2);
 }
 it('should be formatted for fr-CA locale', () => {
  expect(currencyPipeForFR.transform('7500')).toBe('7' + thousandSeparator() + '500 $');
 });
});