[英]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
类型的参数使用any
或captureAny
,但之前未调用 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()
andcaptureAny()
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 useregisterFallbackValue()
to provide a default return value.开箱即用,它会自动处理所有原始类型,但是,当使用参数匹配器代替自定义类型时,开发人员必须使用
registerFallbackValue()
来提供默认返回值。 It is only required to callregisterFallbackValue()
once per type so it is recommended to place allregisterFallbackValue()
calls withinsetUpAll()
.每种类型只需要调用一次
registerFallbackValue()
,因此建议将所有registerFallbackValue()
调用放在setUpAll()
中。
basically you can only use any()
or captureAny()
on String , int , double and other primitive Data Types,基本上你只能在String 、 int 、 double和其他原始数据类型上使用
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.