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

ama*_*mal 2 pipe separator jasmine typescript angular

我正在开发一个 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 $');
 });
});
Run Code Online (Sandbox Code Playgroud)

我收到此错误,

Expected '7 500 $' to be '7 500 $'.
Run Code Online (Sandbox Code Playgroud)

我确实检查了转换结果的实例,它是一个String. 我错过了什么?任何帮助,将不胜感激。

ama*_*mal 5

好吧,罪魁祸首是angular 的内置CurrencyPipe用于“ fr-CA ”语言环境的分组/千位分隔符。而检测用于所述字符串中的每个字符的UTF-16码单位值,我能够看到分组/千分隔符(\ u00A0)在索引1(间75的管的输出值“的7 $ 500 ” ) 与普通的空格键字符 (\u0020) 不同。' $ ' 符号前的空格表示期望值 ' 7 500 $' 等效于 \u0020(普通空格键字符),因为它是在自定义管道的逻辑中手动附加到内置管道的格式化结果中的。

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

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 $');
 });
});
Run Code Online (Sandbox Code Playgroud)