Intercepting Supabase.from in Flutter for testing

88 Views Asked by At

I'm trying to intercept supabase.from() so that I can start testing my Flutter app. But since .execute() as been depreciated, I don't understand how supabase.from().select() works. I tried intercepting the .then() since it always throw me an error saying it hasn't been implemented.

the code gives me this error:

Exception has occurred.
MissingStubError (MissingStubError: 'then'
No stub was found which matches the arguments of this method call:
then(Closure: (List<Map<String, dynamic>>) => dynamic, {onError: Closure: (Object, StackTrace) => dynamic})

Here's my original code:

import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'package:supabase_flutter/supabase_flutter.dart';

import 'supabase_test.mocks.dart';

// Generate a MockClient using the Mockito package.
// Create new instances of this class in each test.
//  dart run build_runner build
@GenerateMocks([
  SupabaseClient,
  SupabaseQueryBuilder,
  PostgrestFilterBuilder,
])
void main() {
  group('Supabase mock', () {
    test("description", () async {
      final supabase = MockSupabaseClient();
      final queryBuilder = MockSupabaseQueryBuilder();
      final filterBuilder = MockPostgrestFilterBuilder<PostgrestList>();

      final PostgrestList mockData = [
        {'email_preferences': true}
      ];

      final mockResponse = PostgrestResponse(data: mockData, status: 200);

      when(supabase.from(any)).thenAnswer((_) => queryBuilder);
      when(queryBuilder.select<PostgrestList>(any)).thenAnswer((_) => filterBuilder);
      when(filterBuilder.then(any)).thenAnswer((_) async => mockResponse);

      final data = await supabase.from("profiles").select<PostgrestList("email_preferences");
      final emailPreferences = data[0]['email_preferences'] as bool;
      expect(emailPreferences, true);
    });
  });
}

I'm new to Flutter testing and testing in general so maybe I'm missing something important. So if someone could provide me with any help, I would be pleased !

0

There are 0 best solutions below