为什么我从bloctest函数中获取空的实际数组?

发布于 2025-02-04 02:22:16 字数 4395 浏览 3 评论 0原文

因此,我正在为我的项目实施bloctesting,并且从特定功能中获得一个空的实际阵列。

share_bloc_test.dart 以下是返回空的实际数组的blocfunction的代码。

    group('ShareDisconnectGrantor', () {
      blocTest<ShareBloc, ShareState>(
        'emits [ShareLoadSuccess] '
        'state when successfully disconnected grantor',
        seed: () => ShareLoadSuccess(ConnectedUsersModel(List<ConnectedUserModel>.filled(1, ConnectedUserModel(1234, '1234', '1234')), List<ConnectedUserModel>.filled(1, ConnectedUserModel(1234, '1234', '1234'))), connectModel: ConnectModel('1234', dateTime, dateTime, dateTime, '1234', '1234')),
        setUp: () {
          when(
            shareRepositoryMock.disconnectFromGrantor(1234, 1234),
          ).thenAnswer(
            (_) => Future<ConnectedUsersModel>.value(ConnectedUsersModel(List<ConnectedUserModel>.filled(1, ConnectedUserModel(1234, '1234', '1234')), List<ConnectedUserModel>.filled(1, ConnectedUserModel(1234, '1234', '1234')))),
          );
        },
        build: () => ShareBloc(
            authenticationBloc: AuthenticationBloc(
                authenticationRepository: authenticationRepositoryMock),
            shareRepository: shareRepositoryMock),
        act: (ShareBloc bloc) => bloc.add(
          const ShareDisconnectGrantor(1234, 1234),
        ),
        expect: () => [
          ShareLoadSuccess(ConnectedUsersModel(List<ConnectedUserModel>.filled(1, ConnectedUserModel(1234, '1234', '1234')), List<ConnectedUserModel>.filled(1, ConnectedUserModel(1234, '1234', '1234'))), connectModel: ConnectModel('1234', dateTime, dateTime, dateTime, '1234', '1234')),
        ],
      );
    });

share_bloc.dart 以下是与事件相对应的函数的代码

  Future<void> _onDisconnectGrantor(
      ShareDisconnectGrantor event, Emitter<ShareState> emit) async {
    if (state is ShareLoadSuccess) {
      final ShareLoadSuccess _currentState = state as ShareLoadSuccess;
      final ConnectedUsersModel _shareUsers = await shareRepository
          .disconnectFromGrantor(event.userId, event.grantorId);

      return emit(_currentState.copyWith(shareUsers: _shareUsers));
    }
  }

。 以下是上述bloctest函数的状态代码,

part of 'share_bloc.dart';

abstract class ShareState extends Equatable {
  const ShareState();

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

class ShareInitial extends ShareState {}

class ShareGranteeSuccess extends ShareState {}

class ShareGranteeFailure extends ShareState {
  final KBMException exception;
  const ShareGranteeFailure({required this.exception});
  @override
  List<Object?> get props => <Object?>[exception];
}

class ShareGrantorSuccess extends ShareState {}

class ShareGrantorFailure extends ShareState {
  final KBMException exception;
  const ShareGrantorFailure({required this.exception});
  @override
  List<Object?> get props => <Object?>[exception];
}

class ShareConnectSuccess extends ShareState {}

class ShareConnectFailure extends ShareState {
  final KBMException exception;
  const ShareConnectFailure({required this.exception});
  @override
  List<Object?> get props => <Object?>[exception];
}

class ShareLoadSuccess extends ShareState {
  final ConnectedUsersModel shareUsers;
  final ConnectModel? connectModel;
  final bool isGrantor;

  const ShareLoadSuccess(this.shareUsers, {this.connectModel, this.isGrantor = false});

  @override
  List<Object?> get props => <Object?>[shareUsers, connectModel, isGrantor];

  ShareLoadSuccess copyWith({
    ConnectedUsersModel? shareUsers,
    ConnectModel? connectModel,
    bool? isGrantor,
  }) {
    return ShareLoadSuccess(
      shareUsers ?? this.shareUsers,
      connectModel: connectModel ?? this.connectModel,
      isGrantor: isGrantor ?? this.isGrantor,
    );
  }
}

class ShareGrantorAborted extends ShareState {}

class ShareSyncInProgress extends ShareState {
  final ConnectModel? connectModel;

  const ShareSyncInProgress({this.connectModel});

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

我有点没有想法。如果有人知道会发生什么,请帮助...

So I'm implementing blocTesting for my project and I'm getting an empty actual array from a specific feature.

share_bloc_test.dart
Below is the code for the blocFunction that is returning the empty actual array.

    group('ShareDisconnectGrantor', () {
      blocTest<ShareBloc, ShareState>(
        'emits [ShareLoadSuccess] '
        'state when successfully disconnected grantor',
        seed: () => ShareLoadSuccess(ConnectedUsersModel(List<ConnectedUserModel>.filled(1, ConnectedUserModel(1234, '1234', '1234')), List<ConnectedUserModel>.filled(1, ConnectedUserModel(1234, '1234', '1234'))), connectModel: ConnectModel('1234', dateTime, dateTime, dateTime, '1234', '1234')),
        setUp: () {
          when(
            shareRepositoryMock.disconnectFromGrantor(1234, 1234),
          ).thenAnswer(
            (_) => Future<ConnectedUsersModel>.value(ConnectedUsersModel(List<ConnectedUserModel>.filled(1, ConnectedUserModel(1234, '1234', '1234')), List<ConnectedUserModel>.filled(1, ConnectedUserModel(1234, '1234', '1234')))),
          );
        },
        build: () => ShareBloc(
            authenticationBloc: AuthenticationBloc(
                authenticationRepository: authenticationRepositoryMock),
            shareRepository: shareRepositoryMock),
        act: (ShareBloc bloc) => bloc.add(
          const ShareDisconnectGrantor(1234, 1234),
        ),
        expect: () => [
          ShareLoadSuccess(ConnectedUsersModel(List<ConnectedUserModel>.filled(1, ConnectedUserModel(1234, '1234', '1234')), List<ConnectedUserModel>.filled(1, ConnectedUserModel(1234, '1234', '1234'))), connectModel: ConnectModel('1234', dateTime, dateTime, dateTime, '1234', '1234')),
        ],
      );
    });

share_bloc.dart
Below is the code for the function corresponding to the event the above code is associated with

  Future<void> _onDisconnectGrantor(
      ShareDisconnectGrantor event, Emitter<ShareState> emit) async {
    if (state is ShareLoadSuccess) {
      final ShareLoadSuccess _currentState = state as ShareLoadSuccess;
      final ConnectedUsersModel _shareUsers = await shareRepository
          .disconnectFromGrantor(event.userId, event.grantorId);

      return emit(_currentState.copyWith(shareUsers: _shareUsers));
    }
  }

share_state.dart
Below is the code for state for the above bloctest function

part of 'share_bloc.dart';

abstract class ShareState extends Equatable {
  const ShareState();

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

class ShareInitial extends ShareState {}

class ShareGranteeSuccess extends ShareState {}

class ShareGranteeFailure extends ShareState {
  final KBMException exception;
  const ShareGranteeFailure({required this.exception});
  @override
  List<Object?> get props => <Object?>[exception];
}

class ShareGrantorSuccess extends ShareState {}

class ShareGrantorFailure extends ShareState {
  final KBMException exception;
  const ShareGrantorFailure({required this.exception});
  @override
  List<Object?> get props => <Object?>[exception];
}

class ShareConnectSuccess extends ShareState {}

class ShareConnectFailure extends ShareState {
  final KBMException exception;
  const ShareConnectFailure({required this.exception});
  @override
  List<Object?> get props => <Object?>[exception];
}

class ShareLoadSuccess extends ShareState {
  final ConnectedUsersModel shareUsers;
  final ConnectModel? connectModel;
  final bool isGrantor;

  const ShareLoadSuccess(this.shareUsers, {this.connectModel, this.isGrantor = false});

  @override
  List<Object?> get props => <Object?>[shareUsers, connectModel, isGrantor];

  ShareLoadSuccess copyWith({
    ConnectedUsersModel? shareUsers,
    ConnectModel? connectModel,
    bool? isGrantor,
  }) {
    return ShareLoadSuccess(
      shareUsers ?? this.shareUsers,
      connectModel: connectModel ?? this.connectModel,
      isGrantor: isGrantor ?? this.isGrantor,
    );
  }
}

class ShareGrantorAborted extends ShareState {}

class ShareSyncInProgress extends ShareState {
  final ConnectModel? connectModel;

  const ShareSyncInProgress({this.connectModel});

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

I kinda have no ideas what might be the issue here. If anyone knows what might be happening please help...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文