Closed dwikyhardi closed 2 months ago
Sorry we cannot debug your issue without a complete reproduction. But answers to specific questions may help: What do you mean by "when I throw exception I got this error"? You say "it's behaving as expected if I run it one by one" so I don't understand what the thrown exception has to do with things... is there a stack trace?
I can't debug it too.
https://github.com/dart-lang/mockito/assets/29680162/47b9a5bf-b713-426c-ac92-6c35dce37cce
Sorry, it's impossible to debug this without the full source. I would try to experiment with commenting out various sets of tests. Maybe comment everything but the test on line 137. Comment all the tests after line 137. Comment everything out except two tests; maybe the test before 137 and the test on 137; maybe the test on 137 and the test after. You can also try throwing a fancy self-calling closure like thenThrow(() { print('throwing 1'); return ServerException(...); }())
.
I still don't know what the problem but moving the test on line 97
to the bottom of the group
fix the error.
I think the problem is something to do with Intercom
or maybe the way I create the mock or method I use for stubbing, but I'm still not sure
😶 Wild! Very glad you moved past the issue. It may still speak to a bug in mockito, but at least you can hopefully move on with your work.
I still don't know what the problem but moving the
test on line 97
to the bottom of thegroup
fix the error. I think the problem is something to do withIntercom
or maybe the way I create the mock or method I use for stubbing, but I'm still not sure
Well, of course it's about when(mockIntercom.updateUser(...))
call, there is the then{Return,Answer}
part of it? ;)
That's what keeps Mockito in the "setting a stub" state, where we don't allow more when
calls.
A good question is how to detect this statically. But that's something to think about for the new API (there it won't be such a big problem anyway, since my plan is to get rid of this part of the global state).
Going to close this.
hey i have this test for my code, it's behaving as expected if I run it one by one in Android studio. but it's throwing error when I run the group test or the whole test.
Here's my test code:
```dart group('getCurrentUser from server', () { test('should return Right(User) when getting current user is successful', () async { when(mockLocalDatasource.saveCurrentUser(mockUser)) .thenAnswer((_) async => Future(() {})); when(mockRemoteDatasource.getCurrentUser()) .thenAnswer((_) async => mockUser); when(mockNetworkInfo.isConnected).thenAnswer((_) async => true); when(mockIntercom.updateUser( userId: mockUser.pk, email: mockUser.username, company: mockUser.organization?.displayName, companyId: mockUser.organization?.pk, name: mockUser.fullName, phone: mockUser.mobile, customAttributes: { 'isOwner': true, 'isAdmin': true, }, )); final result = await authRepository.getCurrentUser(GetDataFromEnum.remoteData); expect(result, equals(Right(mockUser))); expectMethodCall('updateUser', arguments: { 'userId': mockUser.pk, 'email': mockUser.username, 'company': mockUser.organization?.displayName, 'companyId': mockUser.organization?.pk, 'name': mockUser.fullName, 'phone': mockUser.mobile, 'customAttributes': { 'isOwner': true, 'isAdmin': true, }, 'signedUpAt': null, 'language': null, }); verify(mockRemoteDatasource.getCurrentUser()).called(1); verify(mockLocalDatasource.saveCurrentUser(mockUser)).called(1); }); test( 'should return Left(ServerFailure) when getting current user fails because the server throw error', () async { when(mockRemoteDatasource.getCurrentUser()).thenThrow( ServerException(errorCode: 500, message: 'Server Error'), ); when(mockNetworkInfo.isConnected).thenAnswer((_) async => true); final result = await authRepository.getCurrentUser(GetDataFromEnum.remoteData); expect(result, equals(Left(ServerFailure(errorCode: 500, message: 'Server Error')))); verify(mockRemoteDatasource.getCurrentUser()).called(1); verifyNever(mockLocalDatasource.saveCurrentUser(mockUser)).called(0); }); test( 'should return Left(UnauthorizedFailure) when getting current user fails because the JWT token is expired', () async { when(mockRemoteDatasource.getCurrentUser()).thenThrow( UnauthorizedException(message: 'Unauthorized error'), ); when(mockNetworkInfo.isConnected).thenAnswer((_) async => true); final result = await authRepository.getCurrentUser(GetDataFromEnum.remoteData); expect(result, equals(Left(UnauthorizedFailure(message: 'Unauthorized error')))); verify(mockRemoteDatasource.getCurrentUser()).called(1); verifyNever(mockLocalDatasource.saveCurrentUser(mockUser)).called(0); }); test( 'should return Left(NetworkFailure) when getting current user fails because the connection was lost', () async { when(mockNetworkInfo.isConnected).thenAnswer((_) async => false); final result = await authRepository.getCurrentUser(GetDataFromEnum.remoteData); expect(result, isAwhen I throw exception I got this error
the error is located when I called
when
method for adding stub to my method, in this case always the first line in everytest()