Skip to content

Refactor library to be testable

Andrea Ruiz requested to merge feature/refactor_to_testable_library into develop

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;


// Here all elRepo-lib logic

Then, for tests, we have to just do something like:

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.

Merge request reports