我已根据this文章编写了代码n Bloc模式。在我的代码中,我需要连接到实时数据库并从中获取一些数据。但不幸的是,我无法为此编写无错误的单元测试。
这是我的 Res/filename_api_provider
(基于上面的链接)类,提供与firebase的连接条件:
class filenameApiProvider {
var response;
Future<fileModel> fetchList() async {
DatabaseReference ref = FirebaseDatabase.instance.reference();
await ref.child("exercises").once().then((DataSnapshot snap) {
response = snap.value;
});
if (response != null) {
return fileModel.fromJsonWorkouts(response);
} else {
throw Exception('Failed to load workout library api’s');
}
}
,如果响应来自Firebase服务器,则将其传递给模型类以填充一些列表并在UI中显示它们 我试图为此课程编写一个测试,这是我的尝试:
void main() {
test('fetch data', () async {
var obj = new filenameApiProvider();
fileModel result = await obj.fetchWorkoutList();
expect(result, isNotNull);
});
}
但这是运行测试后收到的错误:
MissingPluginException(No implementation found for method Query#observe on channel plugins.flutter.io/firebase_database)
package:flutter/src/services/platform_channel.dart 300:7 MethodChannel.invokeMethod
===== asynchronous gap ===========================
dart:async _AsyncAwaitCompleter.completeError
package:flutter/src/services/platform_channel.dart MethodChannel.invokeMethod
===== asynchronous gap ===========================
dart:async _asyncThenWrapperHelper
package:flutter/src/services/platform_channel.dart MethodChannel.invokeMethod
package:firebase_database/src/query.dart 53:38 Query._observe.<fn>
dart:async Stream.first
package:firebase_database/src/query.dart 90:55 Query.once