gpt4 book ai didi

unit-testing - Flutter 测试通过子文本查找

转载 作者:行者123 更新时间:2023-12-03 02:52:28 33 4
gpt4 key购买 nike

如果找到特定文本作为完整字符串匹配或作为其他文本的子字符串,是否可以断言测试?

只要在任何地方找到字符串 Foo ,即使作为其他字符串的子字符串,我希望以下测试通过。我应该改变什么?

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets('Find sub text', (WidgetTester tester) async {
await tester.pumpWidget(Text('Error code is Foo', textDirection: TextDirection.ltr));
expect(find.text('Error code is Foo'), findsOneWidget);
expect(find.text('Foo'), findsOneWidget);
});
}

迄今为止:
The following TestFailure object was thrown running a test:
Expected: exactly one matching node in the widget tree
Actual: _TextFinder:<zero widgets with text "Foo" (ignoring offstage widgets)>
Which: means none were found but one was expected

最佳答案

代替:

expect(find.text('Foo'), findsOneWidget);

和:
expect(find.byWidgetPredicate((widget) {
if (widget is Text) {
final Text textWidget = widget;
if (textWidget.data != null) return textWidget.data.contains('Foo');
return textWidget.textSpan.toPlainText().contains('Foo');
}
return false;
}), findsOneWidget);

您将首先找到 Text 小部件,然后检查 Text 字符串中的子字符串

关于unit-testing - Flutter 测试通过子文本查找,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62153280/

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