1

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

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());
}

我尝试了 amain_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;
}
4

2 回答 2

0

在我的配置页面中。

@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;
  }
}
于 2021-02-26T15:31:18.010 回答
0
tearDown(() async {
  final getIt = GetIt.instance;

  await getIt.reset();
});
于 2021-06-18T11:14:25.727 回答