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
745 stars 184 forks source link

Testing and mocking guide #864

Open tomasAlabes opened 8 months ago

tomasAlabes commented 8 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.

dshukertjr commented 2 months ago

We just published a package to mock a SupabaseClient instance. We would love to hear what you think about it! https://pub.dev/packages/mock_supabase_http_client

cmoscosoz commented 1 month ago

We just published a package to mock a SupabaseClient instance. We would love to hear what you think about it! https://pub.dev/packages/mock_supabase_http_client

hey @dshukertjr thanks for this. is there any ETA to test .count() and a way to throw errors?

cheers,

dshukertjr commented 1 month ago

@cmoscosoz Glad to hear you like it! There aren't any ETAs, but if you need it, let me add it over the weekend!

a way to throw errors?

For this, what type of feature were you looking for?

cmoscosoz commented 1 month ago

@cmoscosoz Glad to hear you like it! There aren't any ETAs, but if you need it, let me add it over the weekend!

a way to throw errors?

For this, what type of feature were you looking for?

thank you! , i.e: timeouts, or better example something similar like when using when().throw() with mockito  😄

dshukertjr commented 1 month ago

@cmoscosoz I just released v0.0.3 of mock_supabase_http_client, which includes support for count! Let me know if you run into any issues. Also, let me know if you have any additional features that you want it to support.

Let me think about how I can add support for errors 👍

ngoc-quoc-huynh commented 1 month ago

@dshukertjr I would like to test aggregate functions, rpc functions and errors :)

cmoscosoz commented 1 month ago

@cmoscosoz I just released v0.0.3 of mock_supabase_http_client, which includes support for count! Let me know if you run into any issues. Also, let me know if you have any additional features that you want it to support.

Let me think about how I can add support for errors 👍

@dshukertjr awesome thanks! I just created a PR adding support to datetime value types.

cheers!

dshukertjr commented 1 month ago

@ngoc-quoc-huynh Thanks for the suggestion! Will try to add support for them 👍 Keep those feature requests coming if you have more!

@cmoscosoz Thanks for the awesome fix!