haw*_*bee 10 flutter flutter-test
我想测试一些小部件的属性,但我找不到简单的方法。
这是一个带有密码字段的简单示例,我如何检查obviousText是否设置为 true ?
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
const darkBlue = Color.fromARGB(255, 18, 32, 47);
Future<void> main() async {
testWidgets('Wheelio logo appear on the login screen',
(WidgetTester tester) async {
final Key _formKey = GlobalKey<FormState>();
final TextEditingController passwordController = TextEditingController();
const Key _passwordKey = Key('PASSWORD_KEY');
final Finder passwordField = find.byKey(_passwordKey);
await tester.pumpWidget(MaterialApp(
theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue),
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Center(
child: Form(
key: _formKey,
child: TextFormField(
key: _passwordKey,
obscureText: true,
controller: passwordController,
),
),
),
),
));
await tester.pump();
expect(passwordField, findsOneWidget);
final TextFormField myPasswordWidget =
tester.widget(passwordField) as TextFormField;
// How can I check that obscureText property is set to true ?
});
}
Run Code Online (Sandbox Code Playgroud)
Rém*_*let 15
您可以使用tester
和find
来获取小部件树中的任何内容。
例如,如果您想测试 a 的Text
属性是否textAlign
设置为TextAlign.center
,您可以执行以下操作:
expect(
tester.widget(find.byType(Text)),
isA<Text>().having((t) => t.textAlign, 'textAlign', TextAlign.center),
);
Run Code Online (Sandbox Code Playgroud)
小智 5
final passwordField = find.byKey(_passwordKey);
final input = tester.firstWidget<TextFormField>(passwordField);
Run Code Online (Sandbox Code Playgroud)
这input
将是您的小部件,因此您现在可以检查
expect(input.obscureText, true);
归档时间: |
|
查看次数: |
7238 次 |
最近记录: |