Testing 颤振:测试小部件
我想测试的是: 用户点击一个按钮,snackbar显示五秒钟 这是带钥匙的脚手架:Testing 颤振:测试小部件,testing,dart,flutter,Testing,Dart,Flutter,我想测试的是: 用户点击一个按钮,snackbar显示五秒钟 这是带钥匙的脚手架: final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>(debugLabel: "scaffoldState"); 然后,按钮和snackBar的代码: child: FlatButton(key: Key('loginButton'), onPressed: () { validate()
final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>(debugLabel: "scaffoldState");
然后,按钮和snackBar的代码:
child: FlatButton(key: Key('loginButton'), onPressed: () {
validate();
我正试图以这种方式测试这种行为(我也在使用Flutter-redux):
void main(){
final store=store(reducer,initialState:AppState.initialState());
testWidgets(“MaterialApp local”(WidgetTester测试仪)异步{
等待测试仪pumpWidget(新存储提供商(
店:店,,
孩子:MaterialApp(
主页:LoginView(),
)));
等待测试仪。泵和沉降器();
等待测试仪。按(按find.byKey(键(“登录按钮”));
expect();
我不知道该在expect中添加什么。我试图使用find.ByKey(Key))获取GlobalKey并搜索debugLabel,但它似乎不起作用。我从2天以来一直在尝试,但我找不到出路。我实际上遇到了与您相同的条件,我所做的只是将一条文本作为snackbar的内容 为了显示snackbar,我使用的代码是
scaffoldKey.currentState.showSnackBar(SnackBar(content: Text("TAPS!"),));
}, "SEE ALL", backgroundColor: Colors.red, textColor: Colors.blue)
我希望找到与我输入的内容相同的文本,并添加findsOneWidget
expect(find.text(“TAPS!”),findsOneWidget);
回答这个问题可能有点晚了,但由于这是在Google上键入“颤振测试Snackbar”时的第二个结果,我只会给我的两分钱:
其实很简单。不要认为Snackbar完全不同,因为它只在底部停留几秒钟。最后,Snackbar只是另一个小部件
记住这一点,您可以按如下方式进行测试:
testWidgets(“MaterialApp本地”,(WidgetTester测试仪)异步{
...
//测试Snackbar是否出现
expect(find.byType(SnackBar)、findsOneWidget);
//测试它是否包含您的文本
expect(find.text('TAPS!'),findsOneWidget);
});
validate() {
// FormState object can be used to save, reset, and validate
// every FormField that is a descendant of the associated Form.
final FormState form = _formKey.currentState;
if (form.validate()) {
form.save();
form.reset();
//implementation of the snackBar:
_scaffoldKey.currentState.showSnackBar(new SnackBar(
duration: new Duration(seconds: 5)
void main(){
final store = Store<AppState>(reducer, initialState: AppState.initialState());
testWidgets("MaterialApp local", (WidgetTester tester) async{
await tester.pumpWidget(new StoreProvider(
store: store,
child: MaterialApp(
home: LoginView(),
)));
await tester.pumpAndSettle();
await tester.press(find.byKey(Key("loginButton")));
expect();
scaffoldKey.currentState.showSnackBar(SnackBar(content: Text("TAPS!"),));
}, "SEE ALL", backgroundColor: Colors.red, textColor: Colors.blue)