使用 bloc_test 和 mockito 进行颤振测试

时间:2021-02-05 12:14:43

标签: flutter mockito flutter-dependencies bloc flutter-test

我在使用 bloc_test 和 Mockito 时遇到了一些问题。 我有一个具有用例依赖关系的块,我正在创建用例的模拟,其中包含要对其进行测试的值。

这是我的集团:

class IssuesBloc extends Bloc<IssuesEvent, IssuesState> {
  final GetIssues _getIssues;

  IssuesBloc(this._getIssues) : super(IssuesInitial());

  @override
  Stream<IssuesState> mapEventToState(
    IssuesEvent event,
  ) async* {
     // here is my action code that is not the necesary to show for the question of my bloc
    }
}

这是我的 bloc mockito(我不确定如何将 mockito 用例传递给 MockIssuesBloc):

class MockIssuesBloc extends MockBloc<IssuesState> implements IssuesBloc {}

这是我的用例,其中包含要测试的数据列表:

class MockGetIssues extends Mock implements GetIssues {
  Future<Either<AppError, List<IssuesModel>>> call(
      PageParams pageParams) async {
    return Future.delayed(
      const Duration(
        milliseconds: 100,
      ),
      () => right(
        [
          IssuesModel(id: 1),
          IssuesModel(id: 2),
          IssuesModel(id: 3),
          IssuesModel(id: 4),
          IssuesModel(id: 5),
          IssuesModel(id: 6),
          IssuesModel(id: 7),
          IssuesModel(id: 8),
          IssuesModel(id: 9),
          IssuesModel(id: 10),
          IssuesModel(id: 11),
          IssuesModel(id: 12),
          IssuesModel(id: 13),
          IssuesModel(id: 14),
          IssuesModel(id: 15),
          IssuesModel(id: 16),
          IssuesModel(id: 17),
          IssuesModel(id: 18),
          IssuesModel(id: 19),
          IssuesModel(id: 20),
        ].getRange(0, pageParams.take),
      ),
    );
  }
}

这是我的主要测试(我有 3 种不同的方法,但在 3 种方法中出现错误):

void main() {
  group('IssuesBloc', () {
    MockIssuesBloc issuesBloc;
    MockGetIssues mockGetIssues;

    setUpAll(() {
      mockGetIssues = MockGetIssues();
      issuesBloc = MockIssuesBloc();
    });

    blocTest( // throws Bad state: Mock method was not called within `when()`. Was a real method called?
      'bloc: emits [IssuesInitial(), IssuesLoadedState(),] when successful',
      build: () {
        when(getIssues(PageParams()))
            .thenAnswer((value) async => right([IssuesModel(id: 1)]));
        return IssuesBloc(getIssues);
      },
      act: (bloc) => bloc.add(LoadIssues()),
      expect: [
        IssuesInitial(),
        isA<IssuesLoadedState>(),
      ],
    );

    // Bad state: Mock method was not called within `when()`. Was a real method called?
    test('when: emits [IssuesInitial(), IssuesLoadedState(),] when successful', () {
      when(mockGetIssues(PageParams()))
          .thenAnswer((value) async => right([IssuesModel(id: 1)]));
      final bloc = IssuesBloc(mockGetIssues);
      bloc.add(LoadIssues());

      emitsExactly(bloc, [
        IssuesInitial(),
        isA<IssuesLoadedState>(),
      ]);
    });

    // type '_BroadcastSubscription<Type>' is not a subtype of type 'StreamSubscription<IssuesState>'
    test("whenListen: emits [IssuesInitial(), IssuesLoadedState(),] when successful", () {

      whenListen(
        issuesBloc,
        Stream.fromIterable([IssuesInitial]),
      );

      expectLater(
        issuesBloc,
        emitsInOrder(
          [
            IssuesInitial,
            isA<IssuesLoadedState>()
          ], 
        ),
      );
    });
  });
}

我也使用了 bloc 而不使用 mockito,在 3 种情况下我得到下一个错误:状态错误:在 when() 内没有调用 Mock 方法。是否调用了真正的方法? 我不知道为什么我会遇到这个问题

0 个答案:

没有答案