Skip to content

Commit

Permalink
Merge pull request #380 from KnightOfBlackLily/fix/named-skipDoubleRe…
Browse files Browse the repository at this point in the history
…gistration

fix: skipDoubleRegistration with instanceName
  • Loading branch information
escamoteur authored Oct 21, 2024
2 parents 96869c5 + a0d3612 commit e52b9ff
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/get_it_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1586,7 +1586,9 @@ class _GetItImplementation implements GetIt {
);

/// skip double registration
if (skipDoubleRegistration && !allowReassignment) {
if (skipDoubleRegistration &&
!allowReassignment &&
existingTypeRegistration.namedFactories.containsKey(instanceName)) {
return;
}
} else {
Expand Down
21 changes: 21 additions & 0 deletions test/skip_double_registration_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,27 @@ void main() {
expect(getIt<DataStore>(), isA<MockDataStore>());
});

test(' Ignores Double named registration error ', () async {
final getIt = GetIt.instance;
const instanceName = 'named';
getIt.reset();
getIt.allowReassignment = false;
getIt.skipDoubleRegistration = true;
getIt.registerSingleton<DataStore>(RemoteDataStore());
getIt.registerSingleton<DataStore>(
MockDataStore(),
instanceName: instanceName,
);
getIt.registerSingleton<DataStore>(MockDataStore());
getIt.registerSingleton<DataStore>(
RemoteDataStore(),
instanceName: instanceName,
);

expect(getIt<DataStore>(), isA<RemoteDataStore>());
expect(getIt<DataStore>(instanceName: instanceName), isA<MockDataStore>());
});

test(' does not care about [skipDoubleRegistration] varibale ', () async {
final getIt = GetIt.instance;
getIt.reset();
Expand Down

0 comments on commit e52b9ff

Please sign in to comment.