You need to sign in or sign up before continuing.
Refactor library to be testable
It modify all the library to create singleton classes which are mockable using a setter.
With Dart is not possible to mock top level functions nor static ones. With this refactor we are able to maintain top level functions calling a singleton class that do the logic. For tests purposes we can set this singleton class to be a mocked one. For example:
class AccountManagement {
static AccountManagement _accountManagement = AccountManagement._();
factory AccountManagement() {
return _accountManagement;
}
// Used for testing to mock the class
static set instance(AccountManagement val) => _accountManagement = val;
AccountManagement._();
// Here all elRepo-lib logic
}
Then, for tests, we have to just do something like:
@GenerateMocks([AccountManagement.AccountManagement],)
import './account_management_test.mocks.dart';
void main() {
late MockAccountManagement mockAccountManagement;
group('Account management tests', () {
test('test account management can be mocked', () async {
mockAccountManagement = MockAccountManagement();
AccountManagement.AccountManagement.instance = mockAccountManagement;
when(mockAccountManagement.firstPublication()).thenAnswer((_) async => true);
expect(await AccountManagement.firstPublication(), true);
when(mockAccountManagement.firstPublication()).thenAnswer((_) async => false);
expect(await AccountManagement.firstPublication(), false);
});
});
}
So this way we can create specific unit testing for every elRepo-android widget/screen, without mocking the calls from the RsClient
side.