Skip to content

Commit

Permalink
Merge pull request #377 from kuhnroyal/fix/pushScopeInProgress
Browse files Browse the repository at this point in the history
Add tests for pushScopeInProgress
  • Loading branch information
escamoteur authored Oct 21, 2024
2 parents 4c43e0d + 8847731 commit d6cc02f
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions test/scope_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -783,4 +783,62 @@ void main() {
);
expect(getIt<TestClass>(instanceName: 'scope3'), isNotNull);
});

group('should remove scope with error during push', () {
test(
'pushNewScope',
() {
final getIt = GetIt.instance;

expect(
() => getIt.pushNewScope(
scopeName: 'scope1',
init: (getIt) {
getIt.registerSingleton(TestClass());
throw Exception('Error during init');
},
),
throwsException,
);

// The scope should not be on the stack and the registered instance
// should be removed.
expect(getIt.hasScope('scope1'), isFalse);
expect(getIt.isRegistered<TestClass>(), isFalse);

// It should be possible to push a new scope.
getIt.pushNewScope(scopeName: 'scope2');

expect(getIt.hasScope('scope2'), isTrue);
},
);

test(
'pushNewScopeAsync',
() async {
final getIt = GetIt.instance;

await expectLater(
() => getIt.pushNewScopeAsync(
scopeName: 'scope1',
init: (getIt) async {
getIt.registerSingleton(TestClass());
throw Exception('Error during init');
},
),
throwsException,
);

// The scope should not be on the stack and the registered instance
// should be removed.
expect(getIt.hasScope('scope1'), isFalse);
expect(getIt.isRegistered<TestClass>(), isFalse);

// It should be possible to push a new scope.
await getIt.pushNewScopeAsync(scopeName: 'scope2');

expect(getIt.hasScope('scope2'), isTrue);
},
);
});
}

0 comments on commit d6cc02f

Please sign in to comment.