Skip to content

Commit a0d3612

Browse files
fix: skipDoubleRegistration with instanceName
1 parent 14a177f commit a0d3612

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

lib/get_it_impl.dart

+3-1
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

+21
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)