supabase / supabase-flutter

Flutter integration for Supabase. This package makes it simple for developers to build secure and scalable products.
https://supabase.com/
MIT License
661 stars 154 forks source link

Testing and mocking guide #864

Open tomasAlabes opened 3 months ago

tomasAlabes commented 3 months ago

To keep #109 alive, related to #36, https://github.com/supabase/supabase-dart/issues/12.

Any serious project needs unit testing around components using Supabase. In my particular case, I have a provider that fetches data with Supabase Postgrest client, I haven't been able to mock the calls to return mock data.

Some references I've seen, but nothing really works:

An example code to be tested:

class EntitiesProvider with ChangeNotifier {
  final supabase = GetIt.instance.get<SupabaseClient>();
  var _entities = <Entity>[];

  UnmodifiableListView<Entity> entities = UnmodifiableListView(_entities);

  Future<void> fetchEntities() async {
   // some logic
    _entities = await supabase.from("entities").select().map((e) => Entity.fromJson(e)).toList();
   // some logic
  }

}

I know there are ways to work around this, but I'm looking for a blessed and official way from the Supabase team.