简体   繁体   English

错误的 state:测试尝试在 T State 使用 bloc_test 和 mocktail 类型的参数上使用 any 或 captureAny

[英]Bad state: A test tried to use any or captureAny on a parameter of type T State using bloc_test and mocktail

I tried the solutions in here https://github.com/felangel/mocktail/issues/42 but still get error.我在这里尝试了https://github.com/felangel/mocktail/issues/42中的解决方案,但仍然出现错误。 This is my code:这是我的代码:

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(),
      ],
    );
  });
}

And this is the error:这是错误:

Bad state: A test tried to use any or captureAny on a parameter of type AuthenticationState , but registerFallbackValue was not previously called to register a fallback value for AuthenticationState .错误 state:测试尝试对AuthenticationState类型的参数使用anycaptureAny ,但之前未调用 registerFallbackValue 来为AuthenticationState注册后备值。

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

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

This instance of AuthenticationState will only be passed around, but never be interacted with.这个AuthenticationState实例只会被传递,但永远不会被交互。 Therefore, if AuthenticationState is a function, it does not have to return a valid object and could throw unconditionally.因此,如果AuthenticationState是 function,它不必返回有效的 object 并且可以无条件地抛出。 If you cannot easily create an instance of AuthenticationState , consider defining a Fake :如果您不能轻松创建AuthenticationState的实例,请考虑定义Fake

class MyTypeFake extends Fake implements MyType {}

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

What did i miss?我错过了什么?

as said in the docs:如文档中所述:

In order to support argument matchers such as any() and captureAny() mocktail has to register default fallback values to return when the argument matchers are used.为了支持参数匹配器,例如any()captureAny() ),mocktail 必须注册默认的回退值,以便在使用参数匹配器时返回。 Out of the box, it automatically handles all primitive types, however, when using argument matchers in place of custom types developers must use registerFallbackValue() to provide a default return value.开箱即用,它会自动处理所有原始类型,但是,当使用参数匹配器代替自定义类型时,开发人员必须使用registerFallbackValue()来提供默认返回值。 It is only required to call registerFallbackValue() once per type so it is recommended to place all registerFallbackValue() calls within setUpAll() .每种类型只需要调用一次registerFallbackValue() ,因此建议将所有registerFallbackValue()调用放在setUpAll()中。

basically you can only use any() or captureAny() on String , int , double and other primitive Data Types,基本上你只能在Stringintdouble和其他原始数据类型上使用any()captureAny()

for using any on a non primitive data type you should register a default value so mocktail knows what to return;对于在非原始数据类型上使用 any ,您应该注册一个默认值,以便 mocktail 知道要返回什么;

you can create a Fake for ease of use like this您可以像这样创建一个易于使用的 Fake

class Food {...}

class Cat {
  bool likes(Food food) {...}
}

class MockCat extends Mock implements Cat {}

class FakeFood extends Fake implements Food {}

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

  test('...', () {
    final cat = MockCat();
    when(() => cat.likes(any()).thenReturn(true);
    ...
  });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 bloc_test 测试 state class 中的吸气剂? - How to test getters in a state class using bloc_test? 使用 Flutter bloc_test 测试特定状态 - Testing for specific states using Flutter bloc_test Flutter 测试表明 bloc 没有产生任何状态 - Flutter test that no state is yielded from bloc ( Flutter Bloc ) 不符合绑定的 'StateStreamable<state> ' 类型参数 'B'</state> - ( Flutter Bloc ) doesn't conform to the bound 'StateStreamable<state>' of the type parameter 'B' Flutter 测试 Mocktail 错误:“Null”类型不是“Future”类型的子类型<Object?> &#39; - Flutter test Mocktail error : type 'Null' is not a subtype of type 'Future<Object?>' Flutter bloc 测试——emitsInorder 不发出初始状态 - Flutter bloc test -- emitsInorder does not emit the initial state 如何让小部件测试等到 Bloc 更新状态? - How to make widget test wait until Bloc has updated the state? Flutter 集成测试 - 坏 state,无元素 - Flutter integration test - bad state, no element 错误:未为类型“Bloc”定义设置器“observer”<event, state> '</event,> - error: The setter 'observer' isn't defined for the type 'Bloc<Event, State>' 未处理的异常:错误 state:在调用“dispose”后尝试使用 StateNotifier - Unhandled Exception: Bad state: Tried to use StateNotifier after `dispose` was called
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM