gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 00:43:31 24 4
gpt4 key购买 nike

我正在开发一个使用自定义货币管道的 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 用于“fr-CA”语言环境的分组/千位分隔符。在检查字符串中每个字符的 UTF-16 代码单元值时,我能够在索引 1 (在 75 之间的管道输出值 '7 500 $') 与正常的空格键字符 (\u0020) 不同。预期值“7 500 $”中“$”符号之前的空格相当于\u0020(普通空格键字符),因为它是手动附加到在自定义管道的逻辑中内置管道的格式化结果。

因此,作为使用依赖于语言环境的管道(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 $');
});
});

关于angular - Jasmine 测试失败但 'expected' 和 'toBe' 字符串似乎相等?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45248236/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com