English 中文(简体)
利用“彩虹集团”进行特定国家的测试
原标题:Testing for specific states using Flutter bloc_test

我试图测试利用布洛卡模式和集团——试验而释放的具体价值。 目前,在我预计失败时,我能够进行过一次测试。 集体——考试必须只检查所排放的国家类型,而不是具体数值。 是否有办法利用集团——测试一揽子计划来检验我们这些国家的具体价值?

group( [NewsArticleBloc] , () {
    NewsArticleBloc sut;
    MockNewsRepo mockNewsRepo;
    setUp(() {
      mockNewsRepo = new MockNewsRepo();
      sut = NewsArticleBloc(newsRepo: mockNewsRepo);
    });

    tearDown(() => sut.close());

    blocTest(
       Next state check ,
      build: () => sut,
      act: (bloc) {
        when(mockNewsRepo.getTopHeadlines())
            .thenAnswer((_) => Future.value([Article(author:  Josh )]));
        bloc.add(GetTopHeadlines());
      },
      expect: [
        TopHeadlines(topHeadlines: [Article(author:  Micheal )])
                                             //Expecting failure here ^
      ], 
    );
  });
 
最佳回答

如果您乐意使用预发版本:8.0.0-nullsafety.0,你可将预期状态通过expect>。 测试,或可通过<代码>。 • 定期进行Dart/Flutter测试,例如:

// note expect now requires a function that produces a list
expect: () => [
  TypeMatcher<TopHeadlines>(),
]

如果第一个国家的排放量类型如下:TopHeadlines,这将取得成功。 如果你想要核实国家的某些特性,除其类型外,还可以使用:

expect: () => [
  TypeMatcher<TopHeadlines>()
    .having((headlines) => headlines.topHeadlines,  topHeadlines , hasLength(1)) 
    .having((headlines) => headlines.otherProperty,  otherProperty , isNotNull)
]

If you cannot use prerelease versions in your app, Bloc extends Stream, so you can use regular stream testing methods to verify the correct behaviour:

final bloc = ...
final firstStateEmitted = bloc.skip(1).first;  // skip initialState then return the next state
expect(firstStateEmitted, isA<TopHeadlines>());
expect(firstStateEmitted.someProperty, isNotNull);
问题回答

真正有益的答案。 许多感谢。





相关问题
Flutter App cannot be installed on Android TV

I m building a Flutter app that should support Android TV and Mobile devices. Despite Google Play shows that it is supported, I cannot install app on my MiBox device. While trying to install it, both ...

Moving the icon button to the right causes rendering issues

I am trying to align the icon button to the right of the Text field, I tried the row which made the icon button not centered vertically and now I am trying out the Stack widget. Here is my code - ...

热门标签