0

我在这里尝试了解决方案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(() => mockUserRepository.authenticate(username: username, password: password)).thenAnswer((_) async => token);
        return LoginBloc(userRepository: mockUserRepository, authenticationBloc: mockAuthenticationBloc);
      },
      act: (bloc) => bloc.add(LoginButtonPressed(username: username, password: password)),
      expect: () => [
        LoginInitial(),
        LoginLoading(),
      ],
    );
  });
}

这是错误:

错误状态:测试尝试使用anycaptureAny类型的参数AuthenticationState,但之前未调用 registerFallbackValue 来为 注册后备值AuthenticationState

要修复,请执行以下操作:

void main() {
  setUpAll(() {
    registerFallbackValue(/* create a dummy instance of `AuthenticationState` */);
  });
}

这个实例AuthenticationState只会被传递,但永远不会被交互。因此,ifAuthenticationState是一个函数,它不必返回一个有效的对象并且可以无条件地抛出。如果您无法轻松创建 的实例AuthenticationState,请考虑定义一个Fake

class MyTypeFake extends Fake implements MyType {}

void main() {
  setUpAll(() {
    registerFallbackValue(MyTypeFake());
  });
}

我错过了什么?

4

0 回答 0