0

BLoC の非常に複雑な状態クラスがあります ( flutter_blocblock_testを使用します)。テストでは、州の一部をテストして、いくつかの期待に一致させる必要があります。次のようなもの:

final newRecord = Record(/*...*/);

blocTest<ABloc, AState>(
  'create record started',
  build: () => ABloc(),
  seed: () => AState.initial(),
  act: (bloc) => bloc.add(ARecordCreationStarted(newRecord)),
  expect: () => [
    isStateAll<WalletState>({
      (s) => s.creationStatus.values: hasLength(1),
      (s) => s.creationStatus.values.first: Status.loading,
    }),
  ],
);

これisStateAllは、状態の複数の部分を一致させる私の試みです:

Matcher isStateAll<S>(Map<Function(S state), dynamic> matchers) =>
    _IsStateAll(matchers);

class _IsStateAll<S> extends Matcher {
  Map<Function(S state), dynamic> matchers;

  _IsStateAll(this.matchers);

  @override
  Description describe(Description description) =>
      description.add('Runs all matchers on the state');

  @override
  bool matches(item, Map matchState) {
    for (final e in matchers.entries) {
      final Matcher matcher = wrapMatcher(e.value);
      final isMatching = matcher.matches(e.key(item), matchState);
      if (!isMatching) {
        return false;
      }
    }
    return true;
  }
}

動作していますが、エラーメッセージはあまり役に立ちません:

Expected: [<Runs all matchers on the state>]
  Actual: [
            AState:AState(Status.initial, {-1: Record(null, null, Status.initial, 1617027528349,)}, {-1: Status.error})
          ]
   Which: at location [0] is AState:<AState(Status.initial, {-1: Record(null, null, Status.initial, 1617027528349)}, {-1: Status.error})> which does not match Runs all matchers on the state

他にどのようなアプローチがより適しているのでしょうか? このアプローチがすでに優れていると思われる場合は、エラー メッセージの改善にご協力いただけますか?

4

0 に答える 0