1

我试图在一些 youtube 指南(bloc)中做出类似的脚步,并在生产代码中完美运行。bloc 指南显示在构造函数中输入异步函数,但是当我创建一个简单的成功测试时,会出现错误 MissingStubError。

这是请求 API 的用例(模拟):

abstract class UseCase {
  Future<List<String>> call();
}

状态管理类:

abstract class TestState {}

class TestStateLoading extends Equatable implements TestState {
  const TestStateLoading();

  @override
  List<Object?> get props => [];
}

class TestStateLoaded extends Equatable implements TestState {
  final List<String> tests;

  const TestStateLoaded(this.tests);

  @override
  List<Object?> get props => [tests];
}

class TestStateError extends Equatable implements TestState {
  final String error;

  const TestStateError(this.error);

  @override
  List<Object?> get props => [error];
}

这里是演示者。我使用外部库 Rxdart 来使用响应式编程(switchMap):

class StreamTestPresenter implements TestPresenter {
  final UseCase _useCase;

  StreamTestPresenter({required UseCase usecase}) : _useCase = usecase {
    loadData();
  }

  final _controller = StreamController<List<String>>();

  Sink<List<String>> get sink => _controller.sink;

  @override
  Stream<TestState> get testStream => _controller.stream.switchMap(_tests);

  Stream<TestState> _tests(List<String> tests) async* {
    yield TestStateLoading();
    try {
      yield TestStateLoaded(tests);
    } catch (e) {
      yield TestStateError(e.toString());
    }
  }

  Future<void> loadData() async {
    final tests = await _useCase.call();
    sink.add(tests);
  }
}

和测试。在行动部分,我尝试使用 await sut.loadData(); 没有它:

@GenerateMocks([UseCase])
void main() {
  late StreamTestPresenter sut;
  late MockUseCase usecase;

  setUp(() {
    usecase = MockUseCase();
    sut = StreamTestPresenter(usecase: usecase);
    when(usecase.call()).thenAnswer((_) async => ['a', 'b', 'c']);
  });
  test('Should emit correct events on UseCase success', () async {
    //arrange
    final stream = sut.testStream;
    //assert
    expectLater(
      stream,
      emitsInOrder([
        const TestStateLoading(),
        const TestStateLoaded(['a', 'b', 'c']),
      ]),
    );
    //act
  });
}

这是错误:

MissingStubError: 'call'
No stub was found which matches the arguments of this method call:
call()

Add a stub for this method using Mockito's 'when' API, or generate the mock for MockUseCase with 'returnNullOnMissingStub: true'.
package:mockito/src/mock.dart 190:7                    Mock._noSuchMethod
package:mockito/src/mock.dart 184:45                   Mock.noSuchMethod
test\stream_test_presenter_test.mocks.dart 29:14       MockUseCase.call
package:stub_problem/stream_test_presenter.dart 33:34  StreamTestPresenter.loadData
package:stub_problem/stream_test_presenter.dart 13:5   new StreamTestPresenter
test\stream_test_presenter_test.dart 17:11             main.<fn>
===== asynchronous gap ===========================
dart:async                                             _completeOnAsyncError
package:stub_problem/stream_test_presenter.dart        StreamTestPresenter.loadData
package:stub_problem/stream_test_presenter.dart 13:5   new StreamTestPresenter
test\stream_test_presenter_test.dart 17:11             main.<fn>

Obs:当我删除构造函数中的函数并使用 await sut.loadData(); 在 act 部分应用测试时;完美运行。

4

0 回答 0