标签: bloc-test

使用 Flutter 进行块测试,等待异步操作

我的 Flutter 代码中有这个 bloc_test

blocTest<ProjectsBloc, ProjectsState>(
      'emits [ProjectsState.loading(), ProjectsState.succes([])] with empty projects',
      build: () => projectsBloc,
      act: (bloc) => bloc.add(const ProjectsEvent.fetchProjects()),
      wait: const Duration(milliseconds: 2000),
      expect: () => [
        const ProjectsState.loading(),
        const ProjectsState.succes([]),
      ],
    );
Run Code Online (Sandbox Code Playgroud)

如果我不使用等待选项,测试将失败,因为 act 事件将花费 1 秒。使用 wait 我可以确保我们等待足够长的时间,这样测试就可以了。这看起来有点不确定..所以我的问题是,有没有办法删除等待选项并简单地等待直到处理给定的事件?

async-await flutter bloc-test

7
推荐指数
1
解决办法
1969
查看次数

飞镖/颤动测试肘,比较预期和实际的问题

我正在开发一个使用 Wea​​therAPI 的应用程序。我目前无法实施一些工作测试。我尝试遵循 ResoCoders 指南(https://resocoder.com/2019/11/29/bloc-test-tutorial-easier-way-to-test-blocs-in-dart-flutter/)并实际实现了所有状态,块(我用 Cubit 代替)、类、函数……几乎相同。

这是我的测试代码:

blocTest<WeatherCubit, WeatherBaseState>(
      'Cubit emits WeatherLoaded',
      build: () {
        return WeatherCubit(weatherRepository: mockWeatherRepository);
      },
      act: (WeatherCubit cubit) => cubit.getWeather(),
      expect: () => [
        WeatherLoaded(
            temperature: temperature,
            ...
            lat: lat,
            lon: lon)
      ],
    );
Run Code Online (Sandbox Code Playgroud)

这是我从调试控制台得到的错误信息:

Expected: [Instance of 'WeatherLoaded']
  Actual: [Instance of 'WeatherLoaded']
   Which: at location [0] is <Instance of 'WeatherLoaded'> instead of <Instance of 'WeatherLoaded'>

WARNING: Please ensure state instances extend Equatable, override == and hashCode, or implement Comparable.
Alternatively, consider using …
Run Code Online (Sandbox Code Playgroud)

dart flutter bloc bloc-test

5
推荐指数
1
解决办法
3579
查看次数

错误状态:测试尝试使用 bloc_test 和mocktail 对 T State 类型的参数使用 any 或 captureAny

我尝试了这里的解决方案https://github.com/felangel/mocktail/issues/42但仍然出现错误。这是我的代码:

class MockUserRepository extends Mock implements UserRepository {}
class MockAuthenticationBloc extends MockBloc<AuthenticationEvent, AuthenticationState> implements AuthenticationBloc {}
class FakeAuthenticationEvent extends Fake implements AuthenticationEvent {}
class FakeAuthenticationState extends Fake implements AuthenticationState {}

void main() {
  MockUserRepository mockUserRepository;
  MockAuthenticationBloc mockAuthenticationBloc;

  setUp(() {
    mockUserRepository = MockUserRepository();
    mockAuthenticationBloc = MockAuthenticationBloc();
    registerFallbackValue(FakeAuthenticationEvent());
    registerFallbackValue(FakeAuthenticationState());
  });

  group('Login', () {
    final username = 'someusername';
    final password = 'somepassword';
    final token = 'sometoken';
    final loginError = 'Some error message.';

    blocTest('emits [LoginLoading] when successful',
      build: () {
        when(() => …
Run Code Online (Sandbox Code Playgroud)

flutter bloc-test

5
推荐指数
1
解决办法
2367
查看次数

Flutter:比较预期和实际的块测试问题

我有如下测试:

      blocTest(
        'emits [ViewData.loading and ViewData.loaded] when GetRepositories is added',
        build: () {
          when(
            () => useCase.getRepositories(),
          ).thenAnswer((_) async => Right(repositoriesDummyEntityFromRemote));
          return homeBloc;
        },
        act: (HomeBloc bloc) => bloc.add(GetRepositories()),
        expect: () => [
          HomeState(
            statusRepositories: ViewData.loading(message: 'Loading'),
            isShowTitle: false,
          ),
          HomeState(
            statusRepositories:
                ViewData.loaded(data: repositoriesDummyEntityFromRemote),
            isShowTitle: false,
          ),
        ],
      );
Run Code Online (Sandbox Code Playgroud)

当我运行时出现错误:

package:bloc_test/src/bloc_test.dart 229:13  testBloc.<fn>.<fn>
dart:async                                   _completeOnAsyncError
package:bloc_test/src/bloc_test.dart         testBloc.<fn>.<fn>
===== asynchronous gap ===========================
dart:async                                   _asyncThenWrapperHelper
package:bloc_test/src/bloc_test.dart         testBloc.<fn>.<fn>
dart:async                                   runZonedGuarded
package:bloc_test/src/bloc_test.dart 192:13  testBloc.<fn>
package:bloc_test/src/bloc_test.dart 191:5   testBloc.<fn>
dart:async                                   runZoned
package:bloc/src/bloc_overrides.dart 46:26   BlocOverrides.runZoned
package:bloc_test/src/bloc_test.dart 190:23 …
Run Code Online (Sandbox Code Playgroud)

unit-testing flutter bloc-test

3
推荐指数
1
解决办法
1979
查看次数

如何使用 MockBloc 实现小部件测试?

我正在尝试实现小部件测试以测试登录表单。这个测试取决于我使用 MockBloc 模拟的一个块。但是,它会引发以下错误:

??? EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK?????????????????????????????????????????????????????
The following StateError was thrown running a test:
Bad state: No method stub was called from within `when()`. Was a real method called, or perhaps an 
extension method?
Run Code Online (Sandbox Code Playgroud)

我在以下链接中发现了类似的错误,但我不知道这如何帮助我解决我的问题。

我还在gitlub 上查看了以下文件,这是使用 bloc_test 进行小部件测试的示例。该链接可以在 Bloc 库的官方网站上找到 - 特别是在 Flutter 中使用 Bloc 库的 Todos App 中。

但是,该示例bloc_test: ^3.0.1在我使用时使用bloc_test: ^8.0.0,可在此处找到。

这是一个最小的例子:

  • 登录表单小部件
class LoginForm extends StatelessWidget {
  @override
  Widget …
Run Code Online (Sandbox Code Playgroud)

flutter flutter-test widget-test-flutter bloc-test

1
推荐指数
1
解决办法
721
查看次数

Flutter Bloc 测试“bloc.add”null 安全问题

我有一个 flutter bloc_test,由于最近 Flutter 的升级涉及 null 安全性,该测试失败了。

我有以下代码

blocTest('get the usecase name',
        build: () {
          when(() => mockGetUseCaseName(any()))
              .thenAnswer((_) async => Right(name));
          return bloc;
        },
        act: (bloc) => bloc.add(GetUseCaseName(name)),
        verify: (_) => verify(
            () => mockGetUseCaseName(Params(string: name))));
Run Code Online (Sandbox Code Playgroud)

我在“bloc.add”行上收到错误

“无法无条件调用方法‘add’,因为接收者可能为‘null’”

有任何想法吗?

flutter bloc bloc-test

1
推荐指数
1
解决办法
730
查看次数