我正在尝试编写一个测试来检查我的 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();
,但是没有用。