English 中文(简体)
关于冲破中的单位试验的少数问题
原标题:A little question about unit test in flutter

https://codelabs.developers.google.com/codelabs/flutter-app-testing 第6步

In function in file test/favorites_test.dart , the test fails if I changed the i+=2 to i++ and I don t know why. If the total items is less than 6, it can pass too. Why the number is so important in test.

It is a simple app, when tap the close button, just delete from list enter image description here

/test/favorites_test.dart

Widget createFavoritesScreen() => ChangeNotifierProvider<Favorites>(
      create: (context) {
        favoritesList = Favorites();
        return favoritesList;
      },
      child: const MaterialApp(
        home: FavoritesPage(),
      ),
    );

void addItems() {
  for (var i = 0; i < 10; i += 2) {
    favoritesList.add(i);
  }
}

void main() {
  group( Favorites Page Widget Tests , () {
    testWidgets( Test if ListView shows up , (tester) async {
      await tester.pumpWidget(createFavoritesScreen());
      addItems();
      await tester.pumpAndSettle();
      expect(find.byType(ListView), findsOneWidget);
    });

    testWidgets( Testing Remove Button , (tester) async {
      await tester.pumpWidget(createFavoritesScreen());
      addItems();
      await tester.pumpAndSettle();
      var totalItems = tester.widgetList(find.byIcon(Icons.close)).length;
      await tester.tap(find.byIcon(Icons.close).first);
      await tester.pumpAndSettle();
      expect(tester.widgetList(find.byIcon(Icons.close)).length,
          lessThan(totalItems));
      expect(find.text( Removed from favorites. ), findsOneWidget);
    });
  });
}
问题回答

最后,我找到了解决办法。

http://strong>testWidgets, 试验规模为(2400,1800),,view. PhysicalSize。 (我不知道为什么测试页没有完全显示)

tester.view. PhysicalSize = Size(10000,10000)之后,测试通过。

Don t forget to call tester.view.resetPhysicalSize() or it will affect following widget tests.





相关问题
Flutter App cannot be installed on Android TV

I m building a Flutter app that should support Android TV and Mobile devices. Despite Google Play shows that it is supported, I cannot install app on my MiBox device. While trying to install it, both ...

Moving the icon button to the right causes rendering issues

I am trying to align the icon button to the right of the Text field, I tried the row which made the icon button not centered vertically and now I am trying out the Stack widget. Here is my code - ...