New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
When
throws exception when stubbing response
#37
Comments
Weird! For me, the test case passed. My dart version is |
Hi @haashem 👋 Are you using mocktail instead of mockito? I’m guessing you are still using mockito which is why the test is failing. If you’re having trouble after switching to mocktail then can you please provide a complete reproduction sample? Thanks! |
Hi @felangel, |
@haashem that is expected because you have enabled |
Here is the real demo: Add dependencies: IAuthFacade: import 'package:firebase_auth/firebase_auth.dart';
abstract class IAuthFacade {
Future<String> signInWithEmailAndPassword(
{required String email, required String password});
}
class FirebaseAuthFacade implements IAuthFacade {
final FirebaseAuth _firebaseAuth;
FirebaseAuthFacade(this._firebaseAuth);
@override
Future<String> signInWithEmailAndPassword(
{required String email, required String password}) async {
try {
final userCredentails = await _firebaseAuth.signInWithEmailAndPassword(
email: email, password: password);
return userCredentails.credential!.token.toString();
} on FirebaseAuthException catch (e) {
throw Exception('Login Error');
}
}
}
Test case: import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart';
import 'package:mocktail_issue/firebase_auth_facade.dart';
class MockFirebaseAuth extends Mock implements FirebaseAuth {}
class MockAuthResult extends Mock implements UserCredential {}
void main() {
late MockFirebaseAuth mockFirebaseAuth;
late FirebaseAuthFacade authFacade;
setUp(() {
mockFirebaseAuth = MockFirebaseAuth();
authFacade = FirebaseAuthFacade(mockFirebaseAuth);
});
group('Sign in', () {
test('should signin user when credentials are valid', () async {
when(() => mockFirebaseAuth.signInWithEmailAndPassword(
email: 'sad',
password: 'asd')).thenAnswer((_) async => MockAuthResult());
final result = await authFacade.signInWithEmailAndPassword(
email: '', password: '');
expect(result, 'userToken');
verify(() => mockFirebaseAuth.signInWithEmailAndPassword(
email: 'asd', password: 'ads')).called(1);
});
});
} another issue I have in previous I had not to pass dump strings to methods, test('should signin user when credentials are valid', () async {
when(mockFirebaseAuth!)
.calls(#signInWithEmailAndPassword)
.thenAnswer((_) async => MockAuthResult());
final result = await authFacade!
.signInWithEmailAndPassword(email: email, password: password);
expect(result, const Right(unit));
verify(mockFirebaseAuth!).called(#signInWithEmailAndPassword).once();
}); It was much simpler and makes sense, It was not matter the value for parameters, but with new syntax I had to provide dump values! any reason for this change? |
Hi, @felangel thanks for the best package. |
@felangel Is there any update? or any bottlenecks? |
Hi @haashem 👋 I believe the issue with your test is the values for the parameters you are providing in the stub don't match the values of the parameters being provided when the method is being called. This change was made to align with how mockito works. If you want the stub to match any parameter you can use the import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart';
import 'package:mocktail_issue/firebase_auth_facade.dart';
class MockFirebaseAuth extends Mock implements FirebaseAuth {}
class MockAuthResult extends Mock implements UserCredential {}
void main() {
late MockFirebaseAuth mockFirebaseAuth;
late FirebaseAuthFacade authFacade;
setUp(() {
mockFirebaseAuth = MockFirebaseAuth();
authFacade = FirebaseAuthFacade(mockFirebaseAuth);
});
group('Sign in', () {
test('should signin user when credentials are valid', () async {
when(() => mockFirebaseAuth.signInWithEmailAndPassword(
email: any(named: 'email'),
password: any(named: 'password'))).thenAnswer((_) async => MockAuthResult());
final result = await authFacade.signInWithEmailAndPassword(
email: '', password: '');
expect(result, 'userToken');
verify(() => mockFirebaseAuth.signInWithEmailAndPassword(
email: 'asd', password: 'ads')).called(1);
});
});
} Hope that helps 👍 |
Here we have:
then in test file:
[for demonstration, test doesn't do anything]
then it throws
I have upgraded 'bloc_test' to 8.0.0 and while updating the tests because of syntax change, I face the above error.
What am I doing wrong?
Flutter doctor
The text was updated successfully, but these errors were encountered: