@@ -783,4 +783,62 @@ void main() {
783
783
);
784
784
expect (getIt <TestClass >(instanceName: 'scope3' ), isNotNull);
785
785
});
786
+
787
+ group ('should remove scope with error during push' , () {
788
+ test (
789
+ 'pushNewScope' ,
790
+ () {
791
+ final getIt = GetIt .instance;
792
+
793
+ expect (
794
+ () => getIt.pushNewScope (
795
+ scopeName: 'scope1' ,
796
+ init: (getIt) {
797
+ getIt.registerSingleton (TestClass ());
798
+ throw Exception ('Error during init' );
799
+ },
800
+ ),
801
+ throwsException,
802
+ );
803
+
804
+ // The scope should not be on the stack and the registered instance
805
+ // should be removed.
806
+ expect (getIt.hasScope ('scope1' ), isFalse);
807
+ expect (getIt.isRegistered <TestClass >(), isFalse);
808
+
809
+ // It should be possible to push a new scope.
810
+ getIt.pushNewScope (scopeName: 'scope2' );
811
+
812
+ expect (getIt.hasScope ('scope2' ), isTrue);
813
+ },
814
+ );
815
+
816
+ test (
817
+ 'pushNewScopeAsync' ,
818
+ () async {
819
+ final getIt = GetIt .instance;
820
+
821
+ await expectLater (
822
+ () => getIt.pushNewScopeAsync (
823
+ scopeName: 'scope1' ,
824
+ init: (getIt) async {
825
+ getIt.registerSingleton (TestClass ());
826
+ throw Exception ('Error during init' );
827
+ },
828
+ ),
829
+ throwsException,
830
+ );
831
+
832
+ // The scope should not be on the stack and the registered instance
833
+ // should be removed.
834
+ expect (getIt.hasScope ('scope1' ), isFalse);
835
+ expect (getIt.isRegistered <TestClass >(), isFalse);
836
+
837
+ // It should be possible to push a new scope.
838
+ await getIt.pushNewScopeAsync (scopeName: 'scope2' );
839
+
840
+ expect (getIt.hasScope ('scope2' ), isTrue);
841
+ },
842
+ );
843
+ });
786
844
}
0 commit comments