搜索插件、代码仓与 APP 分类

插件详情v0.16.0

firebase_auth_mocks

Firebase Auth 的模拟实现。可与 `google_sign_in_mocks` 一起使用,编写涉及 Firebase 认证的单元测试。

AndroidiOSWebmacOSWindows
近 30 天下载
14.6万
喜欢数
105
Pub 分数
140

安装插件

flutter pub add firebase_auth_mocks

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

一个测试包,提供 Firebase 认证的模拟实现,使开发者能够在不连接实际 Firebase 后端的情况下模拟认证流程。它与 `google_sign_in_mocks` 配合使用,便于在 Flutter 应用中对认证相关逻辑进行单元测试。非常适合在受控环境中测试登录、登出和用户状态变化。该包支持所有主要平台,包括 Android、iOS、Web、macOS 和 Windows,确保跨环境的一致性测试行为。

适合场景

  • 单元测试身份验证流程
  • 模拟用户登录和登出
  • 测试应用程序在不同身份验证状态下的行为
  • 基于用户身份验证验证UI更新
  • 开发离线测试场景

主要特点

  • 模拟 FirebaseAuth 实例
  • 与 google_sign_in_mocks 的集成
  • 平台无关的测试
  • 支持所有 Flutter 平台
  • 测试环境的简易设置

适合对象

  • Flutter 开发人员编写测试
  • 实践TDD或BDD的团队
  • 严重依赖 Firebase Auth 的应用
  • 测试驱动开发工作流程
  • 需要可靠测试的CI/CD流水线

使用建议

  • 不适用于生产环境
  • 需要配套的 google_sign_in_mocks
  • 模拟行为可能无法反映现实世界中的边缘情况
  • 仅限基本身份验证状态模拟
  • 不支持实时身份验证侦听器

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

Firebase Auth Mocks

pub package Unit Tests Buy Me A Coffee

Mocks for Firebase Auth. Use this package with google_sign_in_mocks to write unit tests involving Firebase Authentication.

Usage

A simple usage example. Add firebase_auth_mocks and google_sign_in_mocks to dev dependencies.

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);
}

Features

  • MockFirebaseAuth supports:
    • instantiating in a signed-in state or not: MockFirebaseAuth(signedIn: true/false).
    • firing events on sign-in to authStateChanges and userChanges.
    • signInWithCredential, signInWithEmailAndPassword, signInWithCustomToken, signInAnonymously, createUserWithEmailAndPassword, signInWithPopup and signInWithProvider signs in.
    • sendSignInLinkToEmail, confirmPasswordReset and verifyPasswordResetCode.
    • verifyPhoneNumber resolves codeSent.
    • signOut
    • sendPasswordResetEmail
    • currentUser
    • the ability to throw exceptions using whenCalling(...).on(...).thenThrow(...). See usage details below. Currently these methods are supported: signInWithCredential, signInWithPopup, signInWithProvider, signInWithEmailAndPassword, createUserWithEmailAndPassword, signInWithCustomToken, signInAnonymously, signOut, sendPasswordResetEmail, sendSignInLinkToEmail, confirmPasswordReset, verifyPasswordResetCode. If you need another method supported, feel free to file a ticket, or better propose a PR.
    • pass auth information (uid, custom claims...) to Fake Cloud Firestore for security rules via authForFakeFirestore. See the docs at fake_cloud_firestore for usage.
    • authStateChanges, userChanges, idTokenChanges stream listeners
  • UserCredential contains the provided User with the information of your choice.
  • User supports:
    • updateDisplayName
    • reauthenticateWithCredential
    • updatePassword
    • delete
    • sendEmailVerification
    • getIdToken and getIdTokenResult
    • the ability to throw exceptions on reauthenticateWithCredential, updatePassword, delete, sendEmailVerification, verifyPhoneNumber, linkWithCredential, linkWithProvider, unlink, User.reload, User.verifyBeforeUpdateEmail.

Throwing exceptions

Regardless of the parameters
whenCalling(Invocation.method(#signInWithCredential, null))
  .on(auth)
  .thenThrow(FirebaseAuthException(code: 'bla'));
expect(
  () => auth.signInWithCredential(FakeAuthCredential()),
  throwsA(isA<FirebaseAuthException>()),
);
Depending on positional parameters
Equality
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<FirebaseAuthException>()));
expect(() => auth.verifyPhoneNumber('00-222-4444'),
    returnsNormally);
Using any other matcher

Supports all of the matchers from the Dart matchers library.

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<FirebaseAuthException>()),
);
expect(
  () => auth.confirmPasswordReset(
    code: '1234',
    newPassword: 'password',
  ),
  returnsNormally,
);
Depending on named parameters

You can match some or all named parameters. If you omit a named parameter, the library matches it against anything.

In this example, it will throw an exception if the code contains the String 'code', no matter the value of newPassword.

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<FirebaseAuthException>()),
  );
  expect(
      () => auth.confirmPasswordReset(
            code: '10293',
            newPassword: 'password',
          ),
      returnsNormally);

Compatibility table

firebase_auth firebase_auth_mocks
6.0.0 0.15.0
5.0.0 0.14.0
4.0.0 0.9.0
3.5.0 0.8.7

Features and bugs

Please file feature requests and bugs at the issue tracker.

所属分类

依赖

依赖清单 8 项

源码仓库

使用此插件的项目