You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何解决get_version插件引发的测试用例MissingPluginException问题?

解决Flutter单元测试中调用GetVersion插件出现MissingPluginException的问题

这个问题的核心原因很明确:Flutter单元测试运行在纯Dart虚拟机环境中,没有Android/iOS原生平台的支撑。GetVersion插件底层依赖原生系统的PackageInfo来获取版本号,在单元测试环境下找不到对应的原生实现,因此会抛出MissingPluginException。

下面给你几个可行的解决思路:

方法1:Mock插件调用(推荐,聚焦业务逻辑测试)

我们可以用mockito包模拟GetVersion的返回值,完全绕过原生环境依赖,专注测试版本对比的核心逻辑。

步骤:

  1. 在pubspec.yaml的dev_dependencies中添加mockito依赖:
dev_dependencies:
  flutter_test:
    sdk: flutter
  mockito: ^5.4.0
  1. 编写带Mock的测试代码:
import 'package:flutter_test/flutter_test.dart';
import 'package:get_version/get_version.dart';
import 'package:mockito/mockito.dart';
import 'package:version/version.dart';

// 创建Mock类模拟GetVersion插件行为
class MockGetVersion extends Mock implements GetVersion {}

void main() {
  late MockGetVersion mockGetVersion;

  setUp(() {
    mockGetVersion = MockGetVersion();
    // 预设模拟的当前版本号,比如"1.0.0"
    when(mockGetVersion.projectVersion).thenAnswer((_) async => "1.0.0");
  });

  test('checkAppVersion: 当前版本低于最新版本时返回true', () async {
    final currentVersion = await mockGetVersion.projectVersion;
    const latestVersion = "1.0.1"; // 设为更高的版本
    
    expect(
      checkAppVersionTest(
        Version.parse(currentVersion),
        Version.parse(latestVersion)),
      true);
  });

  test('checkAppVersion: 当前版本等于最新版本时返回false', () async {
    final currentVersion = await mockGetVersion.projectVersion;
    const latestVersion = "1.0.0";
    
    expect(
      checkAppVersionTest(
        Version.parse(currentVersion),
        Version.parse(latestVersion)),
      false);
  });
}

bool checkAppVersionTest(Version currentVersion, Version latestVersion) {
  return currentVersion < latestVersion;
}

方法2:直接传入硬编码版本号(最简方案)

既然你已经验证过硬编码版本号可以正常运行,那可以直接跳过插件调用,在测试中传入固定值,只测试版本对比逻辑:

void main() {
  test('checkAppVersion: 当前版本低于最新版本返回true', () {
    const currentVersion = "1.0.0";
    const latestVersion = "1.0.1";
    
    expect(
      checkAppVersionTest(
        Version.parse(currentVersion),
        Version.parse(latestVersion)),
      true);
  });

  test('checkAppVersion: 当前版本等于最新版本返回false', () {
    const currentVersion = "1.0.0";
    const latestVersion = "1.0.0";
    
    expect(
      checkAppVersionTest(
        Version.parse(currentVersion),
        Version.parse(latestVersion)),
      false);
  });
}

bool checkAppVersionTest(Version currentVersion, Version latestVersion) {
  return currentVersion < latestVersion;
}

方法3:使用集成测试(验证真实插件行为)

如果你需要验证GetVersion插件的真实调用效果(比如确认它能正确获取设备上的应用版本),则需要使用Flutter集成测试——它会在真实设备或模拟器上运行,具备完整的原生环境支持。

简要步骤:

  1. 在pubspec.yaml添加集成测试依赖:
dev_dependencies:
  integration_test:
    sdk: flutter
  flutter_test:
    sdk: flutter
  1. 在test/integration_test目录下创建测试文件version_test.dart:
import 'package:flutter_test/flutter_test.dart';
import 'package:get_version/get_version.dart';
import 'package:integration_test/integration_test.dart';
import 'package:version/version.dart';

void main() {
  IntegrationTestWidgetsFlutterBinding.ensureInitialized();

  testWidgets('验证真实版本对比逻辑', (tester) async {
    String currentVersion = await GetVersion.projectVersion;
    const latestVersion = "1.0.1"; // 替换为你的应用最新版本
    
    expect(
      checkAppVersionTest(
        Version.parse(currentVersion),
        Version.parse(latestVersion)),
      true);
  });
}

bool checkAppVersionTest(Version currentVersion, Version latestVersion) {
  return currentVersion < latestVersion;
}
  1. 运行集成测试:
flutter test integration_test/version_test.dart

为什么硬编码能正常运行?

因为硬编码时,你跳过了GetVersion.projectVersion这个需要原生支持的方法调用,测试只执行了纯Dart的版本对比逻辑,自然不会触发MissingPluginException。

内容的提问来源于stack exchange,提问作者sakina

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 04:55:09