I am using "flutter_secure_storage" package in my flutter app to store token for REST API. My Authentication class is given below:
import 'dart:convert';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:http/http.dart' as http;
import 'package:tutor_finder_frontend/constants/api_path.dart' as APIConstants;
class Auth {
final _storage = FlutterSecureStorage();
var headers = {'Content-Type': 'application/json'};
Future<bool> isLoggedIn() async {
final isLoggedIn = await _storage.containsKey(key: 'token');
return isLoggedIn;
}
logOut() async {
await _storage.delete(key: 'token');
}
Future<bool> createAccount(String email, String password) async {
var body = jsonEncode({'email': email, 'password': password});
var response = await http.post(Uri.parse(APIConstants.CREATE_USER_URL),
headers: headers, body: body);
if (response.statusCode == 201) {
var jsonResponse = jsonDecode(response.body);
var token = jsonResponse['token'];
await _storage.write(key: 'token', value: token);
return true;
}
return false;
}
Future<bool> logIn(String email, String password) async {
var body = jsonEncode({'username': email, 'password': password});
var response = await http.post(Uri.parse(APIConstants.LOGIN_USER_URL),
headers: headers, body: body);
if (response.statusCode == 200) {
var jsonResponse = jsonDecode(response.body);
var token = jsonResponse['token'];
await _storage.write(key: 'token', value: token);
return true;
}
return false;
}
}
I want to test this class. I know about Mock and mockito package in flutter. But I can't figure out how to create mock for FlutterSecureStorage class.
For example, how would I call "logIn" function to store the token and then call "isLoggedIn" function to check if the token is there. Is this even possible? I know this may not be the usual use of Unit test. Should I even do this kind of test?