Writing an integration test for an application. An error occurred while running the test: 
As I understood from the debugger, my SetUp initializes my variables twice. That is, the variables are initialized, the first test is executed, and then SetUp is initialized again before executing the 2nd test. How can this problem be solved? My Test:
class MockGroupProvider extends Mock implements GroupProvider {}
class MockStudentProvider extends Mock implements StudentProvider {}
class MockGroupRepository extends Mock implements GroupRepository {}
class MockDatabase extends Mock implements ObjectBox {}
void main() {
final injector = GetIt.instance;
final provider = MockGroupProvider();
final studentProvider = MockStudentProvider();
final repository = MockGroupRepository();
final db = MockDatabase();
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
setUp(() {
injector.registerSingleton<GroupProvider>(provider);
injector.registerSingleton<StudentProvider>(studentProvider);
injector.registerSingleton<GroupRepository>(repository);
injector.registerSingleton<ObjectBox>(db);
});
testWidgets(
"Not inputting a text and wanting to save group display an error: "
"Group name cannot be empty",
(WidgetTester tester) async {
await tester.pumpWidget(const MaterialApp(home: AddGroupPage()));
const IconData iconBtn = Icons.save;
final saveGroupBtn = find.byIcon(iconBtn);
await tester.tap(saveGroupBtn);
await tester.pumpAndSettle();
expect(find.byType(AddGroupPage), findsOneWidget);
expect(find.byType(GroupsPage), findsNothing);
expect(find.text('Group name cannot be empty'), findsOneWidget);
},
);
testWidgets(
"After inputting a text, go to the display page which contains group that same text ",
(WidgetTester tester) async {
await tester.pumpWidget(const MyApp());
const inputText = 'Group 1';
await tester.enterText(
find.byKey(const Key('add_group_field')), inputText);
const IconData iconBtn = Icons.save;
final saveGroupBtn = find.byIcon(iconBtn);
await tester.tap(saveGroupBtn);
await tester.pumpAndSettle();
expect(find.byType(AddGroupPage), findsNothing);
expect(find.byType(GroupsPage), findsOneWidget);
expect(find.text(inputText), findsOneWidget);
},
);
}
The setUp callback runs before each test, therefore it's registering your instances in GetIt multiple times, which will cause an exception to be thrown.
In your case, GetIt should not be necessary here, since no dependency injection appears necessary for your mocks. Instead, you can simply create a new instance of each of your dependencies before each test:
void main() {
late MockGroupProvider provider;
late MockStudentProvider studentProvider;
late MockGroupRepository repository;
late MockDatabase db;
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
// Before each test, create new instances for each dependency
setUp(() {
provider = MockGroupProvider();
studentProvider = MockStudentProvider();
repository = MockGroupRepository();
db = MockDatabase();
});
// Tests...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With