I’m trying to do unit test with Supabase in Flutter like the code bellow:
import 'package:bloc_test/bloc_test.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/mockito.dart';
import 'package:supabase_flutter/supabase_flutter.dart';
class TestMClass {
SupabaseClient client;
TestMClass({
required this.client,
});
Future<AuthResponse> signinMc() async {
final respnse = await client.auth
.signInWithPassword(email: 'example@gmail', password: 'password');
return respnse;
}
}
class MockSupabaseClient extends Mock implements SupabaseClient {}
void main() async {
final MockSupabaseClient mockClient = MockSupabaseClient();
final TestMClass testMCass = TestMClass(client: mockClient);
setUp(() {});
group('TestAuthBloc', () {
test('description', () async {
final auth = await testMCass.signinMc();
expect(auth, isA<AuthResponse>());
});
});
}
it throw this error:….
type ‘Null’ is not a subtype of type ‘GoTrueClient’
how I can Mock or do unit testing for Supabase functions with Flutter?
2
Answers
You can use Fake class like this:
To Add to what was said above for mocking responses such as
Stream<AuthState>
which is the return type from the onAuthStateChange function, and you, for example wanted to test how your logic responds to different auth responses you can implement static methods that return the different states you want your code to handle:And you can then use it in your test files by importing the fixture and then calling any one of the static methods like this:
SupabaseAuthFixture.authenticated()
and I now have a supabase compliant mock type that I can use in my unit tests, hope this helps!!