admin管理员组

文章数量:1221395

I have tried everything for hours and don't seem to be able to make tests work:


// this is taken from 
// more on this below

class MockAssetLoader extends AssetLoader {
  @override
  Future<Map<String, dynamic>?> load(String path, Locale locale) async {
    return <String, dynamic>{
      'aaa': <String, dynamic>{
        'bbb': <String, dynamic>{
          'ccc': <String, dynamic>{
            'ddd': 'ddd',
            'eee': 'eee',
          },
          'fff': 'fff',
        },
      },
    };
  }
}

void main() {
  group('CarouselWidget test', () {
    // test will fail with or without GoRouter and or MockGoRouterProvider
    late MockGoRouter mockGoRouter;

    setUpAll(
          () async {
        SharedPreferences.setMockInitialValues(
          <String, Object>{'hasRunBefore': true},
        );
        // both TestWidgetsFlutterBinding and WidgetsFlutterBinding to be 100% sure
        TestWidgetsFlutterBinding.ensureInitialized();
        WidgetsFlutterBinding.ensureInitialized();
        await EasyLocalization.ensureInitialized();
        mockGoRouter = MockGoRouter();
      },
    );

    Future<void> buildScreen({
      required WidgetTester tester,
    }) async {
      return tester.pumpWidget(
          MaterialApp(
            localizationsDelegates: const [
              GlobalCupertinoLocalizations.delegate,
              GlobalMaterialLocalizations.delegate,
              GlobalWidgetsLocalizations.delegate,
            ],
            supportedLocales: [Locale('en')],
            locale: const Locale('en'),
            theme: ThemeData(platform: TargetPlatform.android),
            home: AppScaffold(body: MaterialApp(
              localizationsDelegates: const [
                GlobalCupertinoLocalizations.delegate,
                GlobalMaterialLocalizations.delegate,
                GlobalWidgetsLocalizations.delegate,
              ],
              supportedLocales: [Locale('en')],
              locale: const Locale('en'),
              theme: ThemeData(platform: TargetPlatform.android),
              home: AppScaffold(body: child),
            ) EasyLocalization(
                assetLoader: MockAssetLoader(),
            supportedLocales: <Locale>[
              Locale('en'),
              Locale('de'),
              Locale('fr'),
              Locale('it'),
            ],
            startLocale: Locale('en'),
            path: 'assets/translations',
            child: MockGoRouterProvider(
              goRouter: mockGoRouter,
              child: CarouselWidget(
                pages: <CarouselModel>[
                  CarouselModel(
                    image: ImageAssets.carouselFirst(),
                    title: 'First',
                  ),
                  CarouselModel(
                    image: ImageAssets.carouselSecond(),
                    title: 'Second',
                  ),
                  CarouselModel(
                    image: ImageAssets.carouselThird(),
                    title: 'Third',
                  ),
                ],
              ),
            ),
          ),
        ),
        )
      );
    }

    testWidgets(
      'when the widget is rendered '
          'it should display a SomeWidget '
          'it should display an AnimatedContainer '
          'it should display a ButtonPrimary',
          (WidgetTester tester) async {
        when<void>(() => mockGoRouter.go(any())).thenAnswer((_) async {});

        await buildScreen(tester: tester);
        await tester.pumpAndSettle();

        expect(find.byType(SomeWidget), findsWidgets);
        expect(find.byType(AnimatedContainer), findsWidgets);
        expect(find.byType(ButtonPrimary), findsWidgets);
      },
    );
  });
}

This is the output when I run test

[

本文标签: