单个文件中多个测试用例的 Flutter 集成测试失败
Flutter Integration testing failed for multiple test cases in a single file
我有一个带有电子邮件和密码文本字段的简单登录页面。一个登录按钮,另一个注册按钮。我尝试为登录页面编写集成测试。
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);
});
}
当我执行测试用例时,它失败并出现以下错误:
The following ArgumentError was thrown running a test: Invalid
argument(s): Object/factory with type AmenitiesProvider is already
registered inside 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;
}
在我的配置页面中。
@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;
}
}
tearDown(() async {
final getIt = GetIt.instance;
await getIt.reset();
});
我有一个带有电子邮件和密码文本字段的简单登录页面。一个登录按钮,另一个注册按钮。我尝试为登录页面编写集成测试。
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);
});
}
当我执行测试用例时,它失败并出现以下错误:
The following ArgumentError was thrown running a test: Invalid argument(s): Object/factory with type AmenitiesProvider is already registered inside 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;
}
在我的配置页面中。
@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;
}
}
tearDown(() async {
final getIt = GetIt.instance;
await getIt.reset();
});