【问题标题】:Unit Testing hive abstraction layer单元测试蜂巢抽象层
【发布时间】:2020-07-18 02:13:36
【问题描述】:

所以我创建了一个更简单的抽象级别,以便在我的 Flutter 应用程序中使用 Hive。这应该是管理和访问所有蜂巢箱的中心点。由于例如getApplicationDocumentsDirectory 在测试期间不可用,我怎样才能测试整个文件?

import '../services/workout.dart';
import 'package:hive/hive.dart';
import 'package:path_provider/path_provider.dart' as path_rovider;

import 'workout.dart';

class HiveService {
  static final HiveService _singleton = HiveService._internal();

  static const String _workoutBox = "workoutBox";

  factory HiveService() {
    return _singleton;
  }
  HiveService._internal();

  static Future<void> init() async {
    final appDocumentDirectory =
        await path_rovider.getApplicationDocumentsDirectory();
    Hive.init(appDocumentDirectory.path);
    Hive.registerAdapter(WorkoutAdapter());
  }

  static Future openWorkouts() {
    return Hive.openBox<Workout>(_workoutBox);
  }

  static Future close() {
    return Hive.close();
  }
  
}

【问题讨论】:

    标签: flutter unit-testing dart flutter-hive


    【解决方案1】:

    首先,您需要在测试文件中 main 方法的顶部初始化 Hive,然后您可以继续进行其余测试。

    你可以这样使用它:

    void initHive() {
      var path = Directory.current.path;
      Hive.init(path + '/test/hive_testing_path');
    }
    
    main() {
      initHive();
    //The rest of your test code.
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-26
      • 2018-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多