Closed martin-robert-fink closed 3 years ago
Shortly after posting this, I figured it out on my own. I'll document the solution here in case in helps others:
Essentially, it's a simple mechanism to replicate the localization setup in the Material app when testing the widget, like this:
file: widget_test.dart
import 'package:flutter_localizations/flutter_localizations.dart';
import '../lib/generated/S.dart';
void main() {
testWidgets('Test Localized Widget', (WidgetTester tester) async {
await tester.pumpWidget(
MaterialApp(
localizationsDelegates: [
S.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: S.delegate.supportedLocales,
home: LocalizedWidgetToTest(), // Whatever widget needs testing
),
);
await tester.pumpAndSettle(); // Wait for localization delegates to complete
// Whatever you expect to find
expect(find.byType(TextField), findsOneWidget());
});
}
@martin-robert-fink You can also check out tests in Sample app: https://github.com/localizely/flutter-intl-plugin-sample-app/blob/master/test/localized_widget_test.dart
Whenever I try to tester.pumpWidget(myWidget), if myWidget has any calls to S.of(context), then the widget testing fails as a call to NULL. Is there a proper way to do widget testing using Flutter Intl to resolve the context and get the widget tests to work? If there is, perhaps add a short paragraph on widget testing in the docs. Thx.