I have written my code n Bloc pattern based on this article. In my code, I need to connect to a real-time database and fetch some data from it. But unfortunately, I can't write a bug-free unit test for it.
This is my Res/filename_api_provider(based on the above link) class that provide the connection criteria to 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');
}
}
and if the response comes from the Firebase server I pass it to a model class to fill some list and show them in UI I tried to write a test for this class and this is my attempt:
void main() {
test('fetch data', () async {
var obj = new filenameApiProvider();
fileModel result = await obj.fetchWorkoutList();
expect(result, isNotNull);
});
}
But this is an error I receive after running test:
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