I’m currently working on a Dart project where I have a class DiscoverRemoteDataSourceImpl that implements a method getShoes(). This method fetches data from Firebase and returns a list of ShoeModel objects. Here’s the method:
class DiscoverRemoteDataSourceImpl implements DiscoverRemoteDataSource {
DiscoverRemoteDataSourceImpl(this._real);
final FirebaseDatabase _real;
@override
Future<List<ShoeModel>> getShoes() async {
try {
final data = await _real.ref('product').child('products').once();
List<ShoeModel> shoes = [];
for (int i = 0; i < data.snapshot.children.length; i++) {
var shoe =
data.snapshot.children.elementAt(i).value as Map<Object?, Object?>;
Map<String, dynamic> shoeMap = {};
shoe.forEach((key, value) {
shoeMap[key.toString()] = value;
});
shoes.add(ShoeModel.fromMap(
shoeMap)); // Pass the Map<String, dynamic> to ShoeModel.fromMap
}
return shoes;
} catch (e) {
throw CustomFirebaseException(
message: e.toString(),
code: 500,
);
}
}
}
I’m trying to write a unit test for this method using the mocktail package, but I’m not sure how to mock the FirebaseDatabase class and the DataSnapshot class. Can anyone provide an example of how to do this? Any help would be greatly appreciated.