-
-
Notifications
You must be signed in to change notification settings - Fork 81
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
Flutter Secure Storage Mock Throws type 'Null' is not a subtype of type 'Future<String?>' #168
Comments
Hey @DiegoVega19, please format your code first and insert the whole error if you can. 🙏 https://docs.github.com/en/get-started/writing-on-github/getting-started-with-writing-and-formatting-on-github/basic-writing-and-formatting-syntax |
i know it's been a year but i have the same problem here and for the sake of brevity i'll show only the write function.. and the weird thing is that when i test the saveLocalData isolating the storage.write as you'll see below the test passes but inside my authenticateCrewMember it does not..
Auth Provider
AuthProviderTest
Thats the error when I run debug in the test
any help will be very much appreciated.. |
forgot:
|
If anyone is still having this issue can you please provide a link to a minimal reproduction sample? it's much easier for me to help if I'm able to reproduce the issue locally, thanks! |
Hello, could someone suggest me an idea how to solve this?
When im trying to mock flutter secure storage i get this error " type 'Null' is not a subtype of type 'Future<String?> "
-This is my storage
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
abstract class IStorage {
Future saveData(String value, String key);
Future getId(String key);
}
class MyStorage implements IStorage {
MyStorage(this._storage);
final FlutterSecureStorage _storage;
@OverRide
Future getId(String key) async {
return await _storage.read(key: key) ?? 'No data founded';
}
@OverRide
Future saveData(String value, String key) async {
await _storage.write(key: key, value: value);
}
}
-My Service
import 'dart:convert';
import 'package:dio/dio.dart';
import 'package:fixed/shared/storage.dart';
import 'package:flutter/cupertino.dart';
class ApiService {
final String _baseURL = 'https://jsonplaceholder.typicode.com/';
Dio _dio = Dio(BaseOptions());
final IStorage _storage;
ApiService(this._storage, {Dio? dio}) {
_dio = dio ?? Dio();
_dio.options.baseUrl = _baseURL;
}
Future get() async {
String id = await _storage.getId('id');
final response = await _dio.get('${_baseURL}posts/$id');
debugPrint(json.encode(response.data));
return json.encode(response.data);
}
}
My Test File
import 'package:dio/dio.dart';
import 'package:fixed/api/api_service.dart';
import 'package:fixed/shared/storage.dart';
import 'package:fixed/store/store1.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:http_mock_adapter/http_mock_adapter.dart';
import 'package:mocktail/mocktail.dart';
class MockStorage extends Mock implements IStorage {}
class MockStorage2 extends Mock implements MyStorage {}
class MockFlutterSecureStorage extends Mock implements FlutterSecureStorage {}
void main() {
final dio = Dio();
final dioAdapter = DioAdapter(dio: dio, matcher: const UrlRequestMatcher());
late ApiService service;
late MockFlutterSecureStorage mockSecureStorage;
setUp(() {
mockStorage = MockStorage();
mockSecureStorage = MockFlutterSecureStorage();
dio.httpClientAdapter = dioAdapter;
service = ApiService(dio: dio, MyStorage(mockSecureStorage));
});
void mockData() {
dioAdapter.onGet('https://jsonplaceholder.typicode.com/posts/1', (request) {
return request.reply(200, {'data': 'sucessfull'});
});
}
void mockGetId() {
when(() => mockStorage.getId('id'))
.thenAnswer((invocation) => Future.value('1'));
}
group('Test Service', () {
test('Test endpoint', () async {
//IT THROW ERROR
when(() => mockSecureStorage.read(key: 'íd')).thenAnswer((_) async => '');
final data = await service.get();
expect(data, equals('{"data":"sucessfull"}'));
verify(() => mockSecureStorage.read(key: 'id')).called(1);
});
});
}
The text was updated successfully, but these errors were encountered: