Flutter 共享首选项未在测试中初始化

问题描述 投票:0回答:0

我正在尝试编写一个测试来检查我的 ThreadService。它使用首选项并且在应用程序中工作正常,但是当我运行测试时我得到一个

LateInitializationError: Field prefs has not been initialized.
这是测试代码:

late SharedPreferences prefs;
void main() async {
  setUp(() async {
    WidgetsFlutterBinding.ensureInitialized();
    SharedPreferences.setMockInitialValues({
        (...)
    });
    prefs = await SharedPreferences.getInstance();
  });
  test('test', () async {
    final threadService = ThreadService(boardTag: 'pr', threadId: 1008826);

    await threadService.getRoots();
    (...)
  });
}

我试过在测试体内移动

prefs = await SharedPreferences.getInstance();
,但是没有用。

flutter dart unit-testing testing sharedpreferences
© www.soinside.com 2019 - 2024. All rights reserved.