gpt4 book ai didi

testing - 如何使用 testWidgets 测试 Widget 的动态颜色

转载 作者:行者123 更新时间:2023-12-03 14:55:46 25 4
gpt4 key购买 nike

我想为自定义小部件编写单元测试,特别是检查嵌套 Text 的颜色基于 BuildContext 主题的小部件.我如何根据 BuildContext 测试不同的样式?
要测试的小部件:

class ThemedText extends StatelessWidget {
@override
Widget build(BuildContext context) {
final TextStyle inheritedTextStyle = Theme.of(context).textTheme.subtitle;
Color customColor = inheritedTextStyle.color;
if (inheritedTextStyle.color == Colors.blue) {
customColor = Colors.red;
}

return Text(
'Themed',
style: inheritedTextStyle.copyWith(color: customColor),
);
}
}
我想写两个测试:
  • 供应继承TextStyle颜色为 Colors.blue , 并测试 Text小部件以覆盖的颜色呈现(即 Colors.blue )。
  • 供应继承TextStyle颜色不是 Colors.blue (例如 Colors.green ),并测试 Text小部件以继承的颜色呈现(例如 Colors.green )。
  • 最佳答案

    像这样的东西对我有用:

    final textColorFinder = tester.widget<Text>(find.byType(Text));

    expect(textColorFinder.style.color, Colors.red);

    关于testing - 如何使用 testWidgets 测试 Widget 的动态颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57102532/

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