【问题标题】:Is there a way to compare formatted value by NumberFormat?有没有办法通过 NumberFormat 比较格式化值?
【发布时间】:2020-04-26 11:50:02
【问题描述】:

我正在使用intl package 最新版本0.16.1 为货币格式创建单元测试。 我的格式化程序是:final currencyFormat = NumberFormat.simpleCurrency(locale: 'pt_BR');

我的预期结果是:var expected = r'R$ 1,00';。我以这种方式将我的格式化程序应用于值1.0var formatted = currencyFormat.format(1.0) 导致R$ 1,00

当我使用expect(formatted, expected) 测试两个值时,它显示结果不一样。

Expected: 'R$ 1,00'
  Actual: 'R$ 1,00'
   Which: is different.
          Expected: R$ 1,00
            Actual: R$ 1,00
                      ^
           Differ at offset 2

好吧,我花了一些时间发现两个字符串的符文只有一个字符差异。

预期符文:(82, 36, 32, 49, 44, 48, 48)

格式化符文:(82, 36, 160, 49, 44, 48, 48)

我的问题是:如果在格式化时使用了不间断空格,那么在没有文档讨论的情况下如何避免这个错误?

【问题讨论】:

    标签: unit-testing flutter dart dart-intl


    【解决方案1】:

    到目前为止,我实现的唯一方法是将 unicode 字符放入我预期的字符串:var expected = 'R\$\u{00A0}1,00';

    这不是一个好的解决方案,因为如果我们在应用程序中有许多格式化程序并测试所有使用不间断空间的内容,我们必须将我们的预期值放入 ASCII 码。

    【讨论】:

      猜你喜欢
      • 2021-09-01
      • 1970-01-01
      • 2014-07-28
      • 1970-01-01
      • 2019-06-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多