Click here to Skip to main content
16,022,309 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:

Dart
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,
      );
    }
  }
}


What I have tried:

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.
Posted

1 solution

One way you could fake your Firebase connections and activities is through GitHub - atn832/fake_cloud_firestore[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900