Skip to content
This repository has been archived by the owner on May 13, 2023. It is now read-only.

Mock SupabaseClient. #12

Closed
happy-san opened this issue Mar 21, 2021 · 7 comments
Closed

Mock SupabaseClient. #12

happy-san opened this issue Mar 21, 2021 · 7 comments
Labels
enhancement New feature or request

Comments

@happy-san
Copy link
Contributor

happy-san commented Mar 21, 2021

Feature request

Is your feature request related to a problem? Please describe.

Working on a little side-project to make a Todo app. It would be awesome to have a mock client to unit test my Repository class.

Describe the solution you'd like

A MockSupabaseClient that'll be useful for unit testing the code (similar to the MockClient from http package).

Describe alternatives you've considered

Tried to create a mock, but got stuck since this gives me called select('task, status') on null. Also since SupabaseQueryBuilder isn't exposed, I can't mock the response of from('todos').

void main() {
  TodoRepository repo;
  final mockClient = MockSupabaseClient();

  setUp(() {
    when(mockClient.from('todos').select('task, status').execute()).thenAnswer(
      (_) => Future.value(
        PostgrestResponse(
          data: [
            {'task': 't1', 'status': true},
            {'task': 't2', 'status': false},
          ],
          status: 200,
          error: null,
          count: null,
        ),
      ),
    );

    repo = TodoRepository(mockClient);
  });

  //...
}

I can help with this feature but I'll need some guidance along the way. Please let me know.

@happy-san happy-san added the enhancement New feature or request label Mar 21, 2021
@happy-san
Copy link
Contributor Author

Hey, @phamhieu have you considered this?

@phamhieu
Copy link
Member

phamhieu commented Apr 12, 2021

Hi @happy-san, postgrest-dart uses http package so you can use MockClient to write your unit test.

client.from('todos').select('task, status').execute() will translate into https://supabase-url/todos?select=task,status

You can use MockClient to return a json response for that url.

@happy-san
Copy link
Contributor Author

happy-san commented Apr 12, 2021

Thanks, I'll try it out.

@phamhieu If I'm following you correctly, you're referring to this, right?

But wouldn't mocking only work if the execute() function accepted a client? Like,

  Future<PostgrestResponse> execute({
    bool head = false,
    CountOption? count,
    http.Client client,
  }) 

@phamhieu
Copy link
Member

Sorry for confusing you with the word MockClient. I create an example here

@happy-san
Copy link
Contributor Author

Thank you for creating an example. Didn't know that a MockServer could be created, awesome!

@BLipscomb
Copy link

I was wondering about building something like a mock supabase client for my tests, but it was an up-hill battle for me. I moved my database calls to a layer that just talks to the database. For example, I have a dbInsert method that I pass a table name and some records. This method is very simple and just does an insert. My business logic contains a call to dbInsert, and I can easily mock this call in my test.

@fimaljo-inunity
Copy link

_instance._initialized': You must initialize the supabase instance before calling Supabase. how to solve this this error showing while unit test and we using supabase only for aythentication and access token

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants