unit-testing - 有没有办法通过 NumberFormat 比较格式化值?

标签 unit-testing flutter dart dart-intl

我正在使用 intl package 为货币格式创建单元测试最新版本 0.16.1 .
我的格式化程序是:final currencyFormat = NumberFormat.simpleCurrency(locale: 'pt_BR');
我的预期结果是:var expected = r'R$ 1,00'; .我将格式化程序应用于值 1.0这样:var 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

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

预期 rune :(82, 36, 32, 49, 44, 48, 48)
格式化 rune :(82, 36, 160, 49, 44, 48, 48)
我的问题是:如果在格式化时使用不间断空格,当没有文档谈论它时如何避免这个错误?

最佳答案

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

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

关于unit-testing - 有没有办法通过 NumberFormat 比较格式化值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61435553/

相关文章:

dart - 通过增加Dart语言中的z-index的轮播?

c# - 使用经过测试的内部方法对公共(public)方法进行单元测试

flutter - zsh : permission denied when trying to use flutter

c# - 使用 Mock.of 的对象的 Moq 返回值?

flutter - 如何计算列表中双倍数据的总和?

android - 如何在flutter中追加两个列表

flutter - 如何在 BLoC 模式上更改 Widget 时添加动画过渡?

android - Flutter AppBar标题与动画进度条重叠

ruby-on-rails - rails : test single file with rake test without resetting database

java - 与交易模板集成测试手动交易