### Mock Firebase Auth Sign-in with Google Example Source: https://github.com/atn832/firebase_auth_mocks/blob/master/README.md Demonstrates how to mock Google Sign-In and Firebase Auth for unit testing. Ensure firebase_auth_mocks and google_sign_in_mocks are added as dev dependencies. ```dart import 'package:firebase_auth_mocks/firebase_auth_mocks.dart'; import 'package:google_sign_in_mocks/google_sign_in_mocks.dart'; main() { // Mock sign in with Google. final googleSignIn = MockGoogleSignIn(); final signinAccount = await googleSignIn.signIn(); final googleAuth = await signinAccount.authentication; final AuthCredential credential = GoogleAuthProvider.getCredential( accessToken: googleAuth.accessToken, idToken: googleAuth.idToken, ); // Sign in. final user = MockUser( isAnonymous: false, uid: 'someuid', email: 'bob@somedomain.com', displayName: 'Bob', ); final auth = MockFirebaseAuth(mockUser: user); final result = await auth.signInWithCredential(credential); final user = await result.user; print(user.displayName); } ``` -------------------------------- ### Mocking confirmPasswordReset with Named Parameter Matching Source: https://github.com/atn832/firebase_auth_mocks/blob/master/README.md Match specific named parameters like 'code' using Dart matchers. If a named parameter is omitted in the mock setup, it defaults to matching 'anything'. This snippet demonstrates throwing an exception based on the 'code' parameter's content. ```dart whenCalling(Invocation.method( #confirmPasswordReset, null, {#code: contains('code')})) .on(auth) .thenThrow(FirebaseAuthException(code: 'invalid-action-code')); expect( () async => await auth.confirmPasswordReset( code: 'code', newPassword: 'password', ), throwsA(isA()), ); expect( () => auth.confirmPasswordReset( code: '10293', newPassword: 'password', ), returnsNormally); ``` -------------------------------- ### Mocking confirmPasswordReset with Dart Matchers Source: https://github.com/atn832/firebase_auth_mocks/blob/master/README.md Use Dart matchers like 'contains' to conditionally mock method calls. This example throws a FirebaseAuthException if the provided code contains 'code'. It also shows how to test for normal return. ```dart final auth = MockFirebaseAuth(); whenCalling(Invocation.method( #confirmPasswordReset, null, {#code: contains('code')})) .on(auth) .thenThrow(FirebaseAuthException(code: 'invalid-action-code')); expect( () => auth.confirmPasswordReset( code: 'code', newPassword: 'password', ), throwsA(isA()), ); expect( () => auth.confirmPasswordReset( code: '1234', newPassword: 'password', ), returnsNormally, ); ``` -------------------------------- ### Mock FirebaseAuth Throwing Exception Based on Positional Parameter Source: https://github.com/atn832/firebase_auth_mocks/blob/master/README.md Sets up MockFirebaseAuth to throw a FirebaseAuthException when verifyPhoneNumber is called with a specific phone number. Other phone numbers will proceed normally, allowing for targeted error simulation. ```dart final auth = MockFirebaseAuth(); whenCalling(Invocation.method(#verifyPhoneNumber, '12-345-6789')) .on(auth) .thenThrow(FirebaseAuthException(code: 'invalid-action-code')); expect(() => auth.verifyPhoneNumber('12-345-6789'), throwsA(isA())); expect(() => auth.verifyPhoneNumber('00-222-4444'), returnsNormally); ``` -------------------------------- ### Mock FirebaseAuth Throwing Exception Regardless of Parameters Source: https://github.com/atn832/firebase_auth_mocks/blob/master/README.md Configures MockFirebaseAuth to throw a specific FirebaseAuthException when signInWithCredential is called, regardless of the provided arguments. Useful for testing error handling paths. ```dart whenCalling(Invocation.method(#signInWithCredential, null)) .on(auth) .thenThrow(FirebaseAuthException(code: 'bla')); expect( () => auth.signInWithCredential(FakeAuthCredential()), throwsA(isA()), ); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.