Skip to content

Commit e52b9ff

Browse files
authored
Merge pull request #380 from KnightOfBlackLily/fix/named-skipDoubleRegistration
fix: skipDoubleRegistration with instanceName
2 parents 96869c5 + a0d3612 commit e52b9ff

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

lib/get_it_impl.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1586,7 +1586,9 @@ class _GetItImplementation implements GetIt {
15861586
);
15871587

15881588
/// skip double registration
1589-
if (skipDoubleRegistration && !allowReassignment) {
1589+
if (skipDoubleRegistration &&
1590+
!allowReassignment &&
1591+
existingTypeRegistration.namedFactories.containsKey(instanceName)) {
15901592
return;
15911593
}
15921594
} else {

test/skip_double_registration_test.dart

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,27 @@ void main() {
3838
expect(getIt<DataStore>(), isA<MockDataStore>());
3939
});
4040

41+
test(' Ignores Double named registration error ', () async {
42+
final getIt = GetIt.instance;
43+
const instanceName = 'named';
44+
getIt.reset();
45+
getIt.allowReassignment = false;
46+
getIt.skipDoubleRegistration = true;
47+
getIt.registerSingleton<DataStore>(RemoteDataStore());
48+
getIt.registerSingleton<DataStore>(
49+
MockDataStore(),
50+
instanceName: instanceName,
51+
);
52+
getIt.registerSingleton<DataStore>(MockDataStore());
53+
getIt.registerSingleton<DataStore>(
54+
RemoteDataStore(),
55+
instanceName: instanceName,
56+
);
57+
58+
expect(getIt<DataStore>(), isA<RemoteDataStore>());
59+
expect(getIt<DataStore>(instanceName: instanceName), isA<MockDataStore>());
60+
});
61+
4162
test(' does not care about [skipDoubleRegistration] varibale ', () async {
4263
final getIt = GetIt.instance;
4364
getIt.reset();

0 commit comments

Comments
 (0)