If you are testing just the Auth class, it seems to me you should be mocking SharedPreferences (rather than the implementation details of MethodChannels) during the test. To do that, you need to be able to inject a mock that implements SharedPreferences in your Auth class. Right now, Auth is coupled not only to SharedPreferences, but to a particular way of getting a SharedPreferences instance. By removing the latter coupling, you can proceed with testing.
Ignoring async for a minute, here's a simple way of doing that using a global variable. (More structured approaches obviously exist using constructor injection or even a dependency injection framework. Often these lead you to move from static members to instances and instance members.)
// globals.dart
SharedPreferences sharedPreferences = SharedPreferences.getInstance();
// auth.dart
import 'globals.dart';
class Auth {
static String sessionID() {
SharedPreferences prefs = sharedPreferences;
return prefs.getString(SID_KEY);
}
}
// auth_test.dart
import 'package:mockito/mockito.dart';
class MockSharedPreferences extends Mock implements SharedPreferences {}
void main() {
test('Auth knows session ID', () {
var mock = new MockSharedPreferences();
sharedPreferences = mock;
when(mock.getString(SID_KEY)).thenReturn('some_sid');
expect(Auth.sessionID(), 'some_id');
});
}
Reintroducing async, you'll have to work with Future<SharedPreferences> instead of SharedPreferences in the above.
// globals.dart
Future<SharedPreferences> sharedPreferences = SharedPreferences.getInstance();
// auth.dart
import 'globals.dart';
class Auth {
SharedPreferences prefs = await sharedPreferences;
return prefs.getString(SID_KEY);
}
}
// auth_test.dart
import 'package:mockito/mockito.dart';
class MockSharedPreferences extends Mock implements SharedPreferences {}
void main() {
test('Auth knows session ID', () async {
var mock = new MockSharedPreferences();
sharedPreferences = new Future.value(mock);
when(mock.getString(SID_KEY)).thenReturn('some_sid');
expect(await Auth.sessionID(), 'some_id');
});
}