小编Jor*_*zco的帖子

在Flutter上测试shared_preferences

我正在编写一个不稳定的应用程序,并且在编写测试时遇到了这个问题。该方法应该将数据写入TextFields并点击一个将数据保存在SharedPrefs中的按钮:

testWidgets('Click on login saves the credentials',
      (WidgetTester tester) async {

    await tester.pumpWidget(MyApp());

    await tester.enterText(find.byKey(Key('phoneInput')), 'test');
    await tester.enterText(find.byKey(Key('passwordInput')), 'test');
    await tester.tap(find.byIcon(Icons.lock));

    SharedPreferences prefs = await SharedPreferences.getInstance();
    expect(prefs.getString('phone'), 'test');
    expect(prefs.getString('password'), 'test');
  });
Run Code Online (Sandbox Code Playgroud)

此测试将无法获得具有以下错误的SharedPreferences实例:

The following TimeoutException was thrown running a test:
TimeoutException after 0:00:03.500000: The test exceeded the timeout. It may have hung.
Consider using "addTime" to increase the timeout before expensive operations.
Run Code Online (Sandbox Code Playgroud)

更新:似乎问题实际上不是超时,因为即使60秒测试也无法解决SharedPreferences实例。

testing sharedpreferences flutter

7
推荐指数
1
解决办法
384
查看次数

标签 统计

flutter ×1

sharedpreferences ×1

testing ×1