V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
magic3584
V2EX  ›  Flutter

请教个单元测试的问题

  •  1
     
  •   magic3584 · 33 天前 · 903 次点击
    这是一个创建于 33 天前的主题,其中的信息可能已经有所发展或是发生改变。

    最近在学 flutter 的单元测试,目前在跟着官方文档 https://codelabs.developers.google.com/codelabs/flutter-app-testing

    但是今天有个问题,就是在 favorites_test 的时候,如果我把下面文件的 第 24 行 for 循环里的 i+=2 变成 i++, 就会测试失败

    官方的项目在 https://github.com/flutter/codelabs/blob/main/testing_codelab/step_07/test/favorites_test.dart

    不清楚什么原因,找到 flutter dev 的官方 discord 询问也没得到解答。

    第 1 条附言  ·  32 天前

    1

    点击关闭按钮,就会从列表里移除。不知道为什么 i++ 的时候就没生效。

    或者说如果 i<6 左右,i++ 也会生效,但是i<10就会失败。不知道为啥会跟个数有关。

    第 2 条附言  ·  32 天前
    解决了,虽然不知道为什么单元测试时候,屏幕大小也会有影响
    https://stackoverflow.com/a/78222716/4256995
    6 条回复    2024-03-26 22:25:52 +08:00
    Hurriance
        1
    Hurriance  
       33 天前 via iPhone
    这两个算术结果本身就不一样吧
    maichael
        2
    maichael  
       32 天前
    i++和 i+=2 也不等价呀
    lovedebug
        3
    lovedebug  
       32 天前
    i++ 是 i+=1
    magic3584
        4
    magic3584  
    OP
       32 天前
    @Hurriance #1
    @maichael #2
    @lovedebug #3
    我知道 +1 的+2 的区别。请看 append,如果个数多就会失败,不清楚为什么
    maichael
        5
    maichael  
       32 天前   ❤️ 1
    @magic3584
    find.byIcon 默认不会寻找 Offstage 的 node, 修改 physicalSize 某种程度上来说是比较 hack 的方法,毕竟说不准多大的 physicalSize 是合适的。

    更恰当的方式是不跳过 Offstage 的寻找:
    var totalItems = tester.widgetList(find.byIcon(Icons.close, skipOffstage: false)).length;

    具体的你可以参考这两个:
    https://api.flutter.dev/flutter/flutter_test/CommonFinders/byIcon.html
    https://api.flutter.dev/flutter/widgets/Offstage-class.html
    magic3584
        6
    magic3584  
    OP
       32 天前
    @maichael #5
    感谢大佬,主要是不明白模拟器一屏可以显示 10 个但是 testWidget 的时候就不行。
    打印出来了也不知道单位是 dp 还是 px 。
    幸亏我是自己敲的,然后粗心敲成 i++了,否则真就错过这一个知识点了。
    后面跑 integration_test 也失败了,我再看看😭
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2887 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 14:37 · PVG 22:37 · LAX 07:37 · JFK 10:37
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.