New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
I can't mock a SupabaseClient instance #714
Comments
Your code example is quite large so I don't know where exactly the issue happens. Additionally, I'm not really familiar with your mock package. I guess there is some cleanup needed for the isolate we use internally for json decoding. |
Yes, I shall admit that my code is a lot verbose, and this version I posted here had a little bug that I've later found out, because in the decorator used to generate mocks, this one: @GenerateNiceMocks([
MockSpec<SupabaseClient>(),
MockSpec<SupabaseQueryBuilder>(),
MockSpec<PostgrestFilterBuilder<List<Map<String, dynamic>>>>(),
MockSpec<PostgrestResponse<List<Map<String, dynamic>>>>()
])
Regarding the mockito package, it just implements a class getting rid of its required parameters and passing mock parameters to it, then you can simply mock the behavior of the class for example: final mockQueryBuilder = MockSupabaseQueryBuilder();
final mockPostgrestFilterBuilder = MockPostgrestFilterBuilder();
when(mockQueryBuilder.select<List<Map<String, dynamic>>>(any))
.thenAnswer((_) => mockPostgrestFilterBuilder); In this line, both these mock classes were created by mockito, so they don't really need parameters you can just instantiate them, then this My guess of what could be the actual problem could be that mockito doesn't really work that well with isolates or the isolate used for JSON decoding in the actual Supabase source code has some kind of problem. The workaround I used for solving this problem was creating a |
Thanks for the explanation and simplification. I guess the constructor of |
I tried calling the test('Should return a list of languages from the remote database', () async {
// arrange
final fixtureMap = fixture('language_list_fixture.json');
final mockQueryBuilder = MockSupabaseQueryBuilder();
final parsedList = json.decode(fixtureMap) as List<dynamic>;
final expectedResult = parsedList
.map((dynamic item) =>
Map<String, dynamic>.from(item as Map<String, dynamic>))
.toList();
final mockPostgrestFilterBuilder = MockPostgrestFilterBuilder();
final mockPostgrestResponse = MockPostgrestResponse();
// Mock the behavior of `from` method
when(mockDatabaseClient.remote.from(any)).thenAnswer(
(_) => mockQueryBuilder,
);
// Mock the behavior of `select` method to return
// the PostgrestFilterBuilder instance
when(mockQueryBuilder.select<List<dynamic>>(any, any))
.thenAnswer((_) => mockPostgrestFilterBuilder);
// Return your expected result when using the mocked
// PostgrestFilterBuilder instance
when(mockPostgrestFilterBuilder.execute())
.thenAnswer((_) async => mockPostgrestResponse);
// Mock the behavior of `data` getter on PostgrestResponse to return
// your expected result
when(mockPostgrestResponse.data).thenReturn(expectedResult);
// act
final result = await syncRemoteDataSourceImpl.fetchLanguages();
// assert
expect(result, expectedResult);
await mockDatabaseClient.supabaseClient.dispose();
}); but mockito overrides the |
Describe the bug
I'm trying to mock
SupabaseClient
using the mockito package and it keeps giving me this timeout exception:TimeoutException after 0:00:30.000000: Test timed out after 30 seconds. See https://pub.dev/packages/test#timeouts dart:isolate _RawReceivePort._handleMessage
To Reproduce
In order to reproduce this exception, you can use the following test:
The definition of the
DatabaseClientInterface
is the following:The
fixture()
function just reads a string synchronously from the fixtures directory:String fixture(String name) => File('test/fixtures/$name').readAsStringSync();
The structure of the JSON file I'm trying to read (just for more context) is the following:
And here is the
SyncRemoteDatasourceImpl
definition:Expected behavior
The expected behavior would be for the test to pass without throwing a timeout exception.
Version:
On Linux/macOS
Additional context
Since I'm very new to Supabase and everything, I don't really know if this is a bug or if it is my logic that is flawed, or if mocking the
SupabaseClient
is possible in the first place, but I have tried doing this in a lot of different ways and all of them just kept throwing me this timeout exception. Something I might want to mention is that, before doing this way, I started searching for more information about mocking theSupabaseClient
and I came across this old Github issue from thesupabase-dart
package, where the user was trying to achieve something very similar to what I want, and an example of a mock client was proposed in the issue, I used it and I had to adapt it and remove some errors, but then it kept throwing me this timeout exception.Here is the link of the issue I mentioned: supabase/supabase-dart#12
The text was updated successfully, but these errors were encountered: