当我的测试失败时,我在尝试在我的项目中实现blocTesting时面临一个奇怪的错误。内容如下:
Expected: [
SignUpCreateAccountLoading:SignUpCreateAccountLoading(),
SignupInitial:SignupInitial()
]
Actual: [
SignUpCreateAccountLoading:SignUpCreateAccountLoading(),
SignUpCreateAccountFailure:SignUpCreateAccountFailure(type '_AssertionError' is not a subtype of type 'Exception' in type cast, nikunj@gmail.com),
SignupInitial:SignupInitial()
]
我需要在这个项目的块测试中使用真正的api。
下面是blocTest, bloc, blocEvent,blocState和repository文件。
SignupBlocTest
void main() async {
group('SignupBloc', () {
late SignUpBloc signUpBloc;
setUp(() {
signUpBloc = SignUpBloc();
});
test('initial state of the bloc is [AuthenticationInitial]', () {
expect(SignUpBloc().state, SignupInitial());
});
group('SignUpCreateAccount', () {
blocTest<SignUpBloc, SignUpState>(
'emits [SignUpCreateAccountLoading, SignupInitial] '
'state when successfully Signed up',
setUp: () {},
build: () => SignUpBloc(),
act: (SignUpBloc bloc) => bloc.add(const SignUpCreateAccount(
'Nevil', 'abcd', 'nikunj@gmail.com', 'english',),),
wait: const Duration(milliseconds: 10000),
expect: () => [
SignUpCreateAccountLoading(),
SignupInitial(),
],
);
});
});
}
signupBloc
class SignUpBloc extends Bloc<SignUpEvent, SignUpState> {
final SignUpRepository _signUpRepository = SignUpRepository();
SignUpBloc() : super(SignupInitial()) {
// Register events here
on<SignUpCreateAccount>(_onSignUpCreateAccount);
}
Future<void> _onSignUpCreateAccount(SignUpCreateAccount event, Emitter<SignUpState> emit) async {
emit(SignUpCreateAccountLoading());
try {
final bool _success = await _signUpRepository.createAccount(event.firstName, event.lastName, event.eMailAddress, event.language);
if (_success) emit(SignUpCreateAccountSuccess());
} catch (e) {
emit(SignUpCreateAccountFailure(exception: e.toString(), email: event.eMailAddress));
emit(SignupInitial());
}
}
}
Signup_event
part of 'signup_bloc.dart';
abstract class SignUpEvent extends Equatable {
const SignUpEvent();
@override
List<Object> get props => <Object>[];
}
class SignUpCreateAccount extends SignUpEvent {
final String firstName;
final String lastName;
final String eMailAddress;
final String language;
const SignUpCreateAccount(this.firstName, this.lastName, this.eMailAddress, this.language);
@override
List<Object> get props => <Object>[firstName, lastName, eMailAddress, language];
}
Signup_state
part of 'signup_bloc.dart';
abstract class SignUpState extends Equatable {
const SignUpState();
@override
List<Object> get props => <Object>[];
}
class SignupInitial extends SignUpState {}
class SignUpCreateAccountLoading extends SignUpState {}
class SignUpCreateAccountSuccess extends SignUpState {}
class SignUpCreateAccountFailure extends SignUpState {
final String exception;
final String email;
const SignUpCreateAccountFailure({required this.exception, required this.email});
@override
List<Object> get props => <Object>[exception, email];
}
Signuprepository
class SignUpRepository {
Future<bool> createAccount(String _firstName, String _lastName, String _eMailAddress, String _language) async {
final Response _response;
try {
_response = await CEApiRequest().post(
Endpoints.createCustomerAPI,
jsonData: <String, dynamic>{
'firstName': _firstName,
'lastName': _lastName,
'email': _eMailAddress,
'language': _language,
'responseUrl': Endpoints.flutterAddress,
},
);
final Map<String, dynamic> _customerMap = jsonDecode(_response.body);
final CustomerModel _clients = CustomerModel.fromJson(_customerMap['data']);
if (_clients.id != null) {
return true;
} else {
return false;
}
} catch (e) {
final MYException _exception = e as MYException;
throw _exception;
}
}
}
某个地方正在抛出_AssertionError
,但您试图将其转换为catch
中的异常。相反,您应该重新抛出预期的异常,并以不同的方式处理其他类型的异常。下面我返回false,但是您可以选择合适的行为来满足您的需要。
try {
...
} on MYException catch (e) {
rethrow;
} catch (e) {
// Not a MYException so returning false.
return false;
}