0

我有一个非常复杂的 BLoC 状态类(我们使用flutter_blocbloc_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