fluttercommunity/rx_command

Unit Testing commands

ilap opened this issue · 1 comments

ilap commented

Hi,

Why don't the following command invoke the service call?

import 'dart:async';

import 'package:mockito/mockito.dart';
import 'package:mockito/src/mock.dart';
import 'package:rx_command/rx_command.dart';
import 'package:test/test.dart';

abstract class Service {
  Future<List<String>> getStringArray(String str);
}

abstract class Interactor {
  RxCommand<void, List<String>> command;
}

class InteractorImpl implements Interactor {
  Service _service;
  @override
  RxCommand<void, List<String>> command;

  InteractorImpl(this._service) {
    print(this._service);
    command = RxCommand.createAsyncNoParam<List<String>>(() async {
      var result = await _service.getStringArray('Get String');
      print('Command' + result?.toString());
      return result;
    }, emitLastResult: true);
  }
}

class MockService extends Mock implements Service {}

main() {
  test('Should emit an emnpty String array.', () async {
    final service = MockService();
    final interactor = InteractorImpl(service);
    final future = Future.sync(() => <String>[]);

    when(service.getStringArray(any)).thenAnswer((_) {
      print('getStringArray');
      return future;
    });

    expect(interactor.command.lastResult, emits([]));
    interactor.command;

    await new Future.delayed(Duration(milliseconds: 500));
  });
}

Error message:

MockService
Expected: should emit an event that []
  Actual: <null>
   Which: was not a Stream or a StreamQueue
package:test_api                expect
test/banking/ui/test.dart 44:5  main.<fn>
===== asynchronous gap ===========================
dart:async                      _AsyncAwaitCompleter.completeError
test/banking/ui/test.dart       main.<fn>
ilap commented

Sorry it's fixed by /w the following

    expect(interactor.command, emits([]));
    interactor.command.execute();