Skip to content

Commit

Permalink
Add tests for pushScopeInProgress
Browse files Browse the repository at this point in the history
  • Loading branch information
kuhnroyal committed Oct 1, 2024
1 parent 14a177f commit 8847731
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 8847731

Please sign in to comment.