【问题标题】:Flutter Integration testing failed for multiple test cases in a single file单个文件中多个测试用例的 Flutter 集成测试失败
【发布时间】:2021-05-19 13:15:40
【问题描述】:

我有一个带有电子邮件和密码文本字段的简单登录页面。一个按钮用于登录,另一个用于注册。我尝试为登录页面编写集成测试。

void main() {
  IntegrationTestWidgetsFlutterBinding.ensureInitialized();

  testWidgets('''could type email and password in text filed''',
      (WidgetTester tester) async {
    await app.main();
    await tester.pumpAndSettle();

    final textFieldEmail = find.byType(InputTextWidget).first;
    final textFieldPassword = find.byType(InputTextWidget).last;

    await tester.enterText(textFieldEmail, "asis.adh@gmail.com");
    await tester.pumpAndSettle();

    expect(find.text("asis.adh@gmail.com"), findsOneWidget);
  });

testWidgets(
      'should redirect to Sign Up Page when create an account is tapped',
      (WidgetTester tester) async {
    await app.main();
    await tester.pumpAndSettle();

    final createAnAccount = find.text("Create an account");
    await tester.tap(createAnAccount);

    await tester.pumpAndSettle();

    expect(find.byType(SignupPage), findsOneWidget);
    expect(find.byType(LoginPage), findsNothing);
  });
}

当我执行测试用例时,它失败并出现以下错误:

在运行测试时引发以下 ArgumentError:无效 参数:类型为便利设施提供者的对象/工厂已经存在 在 GetIt 中注册。

这是我的main.dart 文件

Future main() async {
  WidgetsFlutterBinding.ensureInitialized();
  configureInjection(Environment.prod);

  /// for registering the factory.
  await Future.delayed(const Duration(seconds: 2));
  runApp(RoopaApp());
}

我尝试使用main_test.dart 并添加configureInjection(Environment.test),但没有任何变化。我不确定如何解决该错误。有没有办法在进入新的测试用例之前清理app 或销毁它。如果我将两个测试用例合二为一,那么它可以正常工作。

这里是configureInjection

@injectableInit
void configureInjection(String environment) {
  $initGetIt(getIt, environment: environment);
}

我正在使用 get_it 和注入包进行依赖注入。

这是自动生成的 initGetIt

GetIt $initGetIt(
  GetIt get, {
  String environment,
  EnvironmentFilter environmentFilter,
}) {
  final gh = GetItHelper(get, environment, environmentFilter);
  final httpClientInjectableModule = _$HttpClientInjectableModule();
  final flutterStorageModule = _$FlutterStorageModule();
  gh.lazySingleton<AmenitiesProvider>(() => AmenitiesProvider());
  gh.factory<Client>(() => httpClientInjectableModule.client);
  gh.lazySingleton<FileProvider>(() => FileProvider());
  gh.lazySingleton<FlutterSecureStorage>(
      () => flutterStorageModule.secureStorate);
  gh.factory<ProfilePageBloc>(() => ProfilePageBloc());
  gh.factory<SplashScreenBloc>(() => SplashScreenBloc());
  gh.lazySingleton<AuthLocalDataSourceProtocol>(
      () => AuthLocalDataSource(secureStorage: get<FlutterSecureStorage>()));
  gh.lazySingleton<AuthRemoteDataSourceProtocol>(
      () => AuthRemoteDataSource(client: get<Client>()));
  return get;
}

【问题讨论】:

    标签: flutter dependency-injection integration-testing testcase widget-test-flutter


    【解决方案1】:
    tearDown(() async {
      final getIt = GetIt.instance;
    
      await getIt.reset();
    });
    

    【讨论】:

      【解决方案2】:

      在我的配置页面中。

      @injectableInit
      void configureInjection(String environment) {
        $initGetIt(getIt, environment: environment);
      }
      

      我刚刚创建了一个测试环境并添加了以下内容,现在它可以按预期工作了。

      @injectableInit
      void configureInjection(String environment) {
        $initGetIt(getIt, environment: environment);
        if (environment == Environment.test) {
          getIt.allowReassignment = true;
        }
      }
      

      【讨论】:

        猜你喜欢
        • 2020-03-18
        • 1970-01-01
        • 2022-06-28
        • 1970-01-01
        • 2017-03-29
        • 1970-01-01
        • 2021-09-18
        • 1970-01-01
        • 2020-10-27
        相关资源
        最近更新 更多