我正在尝试测试密码是否被遮挡,这就是我走了多远,但似乎颤振可以读取文本,即使它被遮挡。
testWidgets('password must be hidden', (WidgetTester tester) async {
await tester.pumpWidget(wrapWithMaterialApp(child: page));
await tester.enterText(find.byKey(Key('pass')), '1234');
final passFinder = find.text('1234');
expect(passFinder, findsNothing);
});
Run Code Online (Sandbox Code Playgroud)
测试实际上找到了“1234”,但我完全确定它被掩盖了。
StatefulWidget我正在尝试使用以下函数来测试 a onPressed。
floatingActionButton: FloatingActionButton(
child: Icon(Icons.camera_alt),
onPressed: () async {
try {
final filename = widget.randomStringGenerator?.call() ?? '';
final path = join(
(await getTemporaryDirectory()).path,
'${filename}.png',
);
await widget.cameraController?.takePicture(path);
final file = File(path);
final image = await file.readAsBytes();
widget.onPhotoTaken(image);
await file.delete();
} catch (e) {
print(e);
}
},
),
Run Code Online (Sandbox Code Playgroud)
以及以下小部件测试:
void main() {
Directory directory;
group('PhotoWidget test', () {
setUp(() async {
// Create a temporary directory.
directory = await Directory.systemTemp.createTemp();
// Mock out the MethodChannel for the …Run Code Online (Sandbox Code Playgroud) 我有一个类似于以下内容的小部件测试:
testWidgets('Widget test', (WidgetTester tester) async {
provideMockedNetworkImages(() async {
final widget = MyWidget();
await WidgetTestFunctions.pumpWidgetTest(
tester,
widget,
);
// ....
await tester.tap(find.byType(MyWidget));
await tester.pump(new Duration(milliseconds: 3000));
// ....
expect(widget.value, myValue);
});
});
Run Code Online (Sandbox Code Playgroud)
以及小部件 on-tap 方法的以下实现:
_onButtonPressed() async {
await animationController.forward();
setState(() {
// ...
// Calls method that changes the widget value.
});
}
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是,animationController.forward()在测试中调用该方法后,该setState部分未执行。我应该如何等待这个方法正确完成?在应用程序运行时,这部分代码被正确调用。
似乎await tester.pump(new Duration(milliseconds: 3000));工作不正常,动画的持续时间为 1500 毫秒,并且您可以看到泵持续时间是双倍的。
赏金信息:如果出现以下情况,我会接受您的回答:
\ndo this instead\n\n[编辑:07/02/21] 在flutter 社区 上关注 Miyoyo#5957 ,\ndiscord
\n\n@iapicca Convert widget position to global, get width height, add both, and see if the resulting bottom right position is on screen?并使用以下答案作为参考:
给出下面的代码示例(也可以在 dartpad 上运行)
\nimport \'package:flutter_test/flutter_test.dart\';\nimport \'package:flutter/material.dart\';\n\nfinal _testKey = GlobalKey();\nconst _fabKey = ValueKey(\'fab\');\nfinal _onScreen = ValueNotifier<bool>(true);\n\nvoid main() => runApp(_myApp);\n\nconst _myApp = …Run Code Online (Sandbox Code Playgroud) 我正在尝试对我的项目使用小部件测试,测试工作正常,直到达到我在实际页面中使用 http 请求的程度,我认为忽略该请求
final account = await http.post(
'http://10.0.2.2:5000/LogIn',
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
body: json.encode({'email': email, 'password': password}),
);
Run Code Online (Sandbox Code Playgroud)
account.body 返回空,但在使用模拟器期间运行良好
testWidgets("Successful Sign In", (WidgetTester tester) async {
await tester.pumpWidget(MaterialApp(home: SignIn()));
//1-find widgets needed
final textfieldemail = find.byKey(Key("emailtextformfield"));
expect(textfieldemail, findsOneWidget);
final textfieldpassword = find.byKey(Key("passwordtextformfield"));
expect(textfieldpassword, findsOneWidget);
final buttonsignin = find.byKey(Key("Signin"));
expect(buttonsignin, findsOneWidget);
//2-execute the actual test
await tester.enterText(textfieldemail, "Weaam.wewe91@gmail.com");
await tester.enterText(textfieldpassword, "Weaam@91");
await tester.tap(buttonsignin);
await tester.pump(Duration(seconds: 5));
//await tester.pump(Duration(seconds: 5));
//await _navigateToAccountHome(tester);
//3-check results
expect(find.byType(DeliveryHome), findsOneWidget);
}); …Run Code Online (Sandbox Code Playgroud) 我正在尝试实现小部件测试以测试登录表单。这个测试取决于我使用 MockBloc 模拟的一个块。但是,它会引发以下错误:
??? EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK?????????????????????????????????????????????????????
The following StateError was thrown running a test:
Bad state: No method stub was called from within `when()`. Was a real method called, or perhaps an
extension method?
Run Code Online (Sandbox Code Playgroud)
我在以下链接中发现了类似的错误,但我不知道这如何帮助我解决我的问题。
我还在gitlub 上查看了以下文件,这是使用 bloc_test 进行小部件测试的示例。该链接可以在 Bloc 库的官方网站上找到 - 特别是在 Flutter 中使用 Bloc 库的 Todos App 中。
但是,该示例bloc_test: ^3.0.1在我使用时使用bloc_test: ^8.0.0,可在此处找到。
这是一个最小的例子:
class LoginForm extends StatelessWidget {
@override
Widget …Run Code Online (Sandbox Code Playgroud) 这是我第一次测试 flutter 应用程序。HomeView我尝试用这个测试代码来泵:
void main() {\n group('Home Test', () {\n _pumpHome(WidgetTester tester) => tester.pumpWidget(\n MaterialApp(\n home: HomeView(),\n ),\n );\n\n testWidgets('Route to Azkar page', (WidgetTester tester) async {\n await _pumpHome(tester);\n await tester.tap(find.byKey(Key('morning')));\n expect(find.byType(ListView), findsOneWidget);\n });\n });\n}\nRun Code Online (Sandbox Code Playgroud)\n但这两个例外却发生了。
\n\xe2\x95\x90\xe2\x95\x90\xe2\x95\xa1 EXCEPTION CAUGHT BY WIDGETS LIBRARY \xe2\x95\x9e\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\nThe following LateError was thrown building HomeView(dirty):\nLateInitializationError: Field '_instance@99075166' has not been initialized.\n\nThe relevant error-causing widget was:\n HomeView file:///E:/projects/flutterProject/tafra/test/home_test.dart:26:19\n\nWhen the exception was thrown, this was the stack:\n#0 ScreenUtil._instance (package:flutter_screenutil/screen_util.dart)\n#1 new ScreenUtil (package:flutter_screenutil/screen_util.dart:28:12)\n#2 SizeExtension.w …Run Code Online (Sandbox Code Playgroud) testing unit-testing flutter flutter-test widget-test-flutter
flutter ×6
dart ×3
flutter-test ×3
testing ×2
animation ×1
bloc-test ×1
unit-testing ×1
widget ×1