简体   繁体   English

如何实现 BlocTest function?

[英]How to implement BlocTest function?

I'm trying to implement blocTesting for my flutter app starting with authentication feature.我正在尝试从身份验证功能开始为我的 flutter 应用程序实施 blocTesting。 Below are the Authentication and login related files required for this.以下是为此所需的身份验证和登录相关文件。 I'd really appreciate if someone could show me on how I can implement blocTesting based on my code because I've been facing problems in doing so.如果有人能告诉我如何根据我的代码实现 blocTesting,我将非常感激,因为我在这样做时遇到了问题。 Below are the bloc, state and event files for the auth bloc.以下是认证集团的区块、state 和事件文件。

Authbloc.dart Authbloc.dart

 import 'dart:async'; import 'package:bloc/bloc.dart'; import 'package:equatable/equatable.dart'; part 'authentication_event.dart'; part 'authentication_state.dart'; class AuthenticationBloc extends Bloc<AuthenticationEvent, AuthenticationState> { final AuthenticationRepository authenticationRepository = AuthenticationRepository(); final SettingsRepository _settingsRepository = SettingsRepository(); AuthenticationBloc(): super(AuthenticationInitial()) { // Register events here on<AuthenticationStarted>(_onAuthenticationStarted); on<AuthenticationLoggedIn>(_onAuthenticationLoggedIn); on<AuthenticationLoggedOut>(_onAuthenticationLoggedOut); } Future<void> _onAuthenticationStarted(AuthenticationStarted event, Emitter<AuthenticationState> emit) async { try { final bool hasToken = await authenticationRepository.hasToken(); if (hasToken) { final Settings _settings = await _settingsRepository.getSettings(); final SysConfig _sysConfig = await _settingsRepository.getSysconfig(); final CountriesModelList _countries = await _settingsRepository.getCountries(); final ReasonsModelList _reasons = await _settingsRepository.getReasons(); final NotificationOptionsList _notificationOptions = await _settingsRepository.getNotificationOptions(); emit( AuthenticationLoadSuccess( settings: _settings, sysConfig: _sysConfig, countries: _countries, reasons: _reasons, notificationOptions: _notificationOptions, ), ); } else { emit(AuthenticationUnauthenticated()); } } catch (e) { final MYException _exception = e as MYException; emit(AuthenticationLoadFailure(exception: _exception)); } } Future<void> _onAuthenticationLoggedIn(AuthenticationLoggedIn event, Emitter<AuthenticationState> emit) async { emit(AuthenticationLoadInProgress()); await authenticationRepository.persistToken(event.token); final Settings _settings = await _settingsRepository.getSettings(); final SysConfig _sysConfig = await _settingsRepository.getSysconfig(); final CountriesModelList _countries = await _settingsRepository.getCountries(); final ReasonsModelList _reasons = await _settingsRepository.getReasons(); final NotificationOptionsList _notificationOptions = await _settingsRepository.getNotificationOptions(); emit( AuthenticationLoadSuccess( settings: _settings, sysConfig: _sysConfig, countries: _countries, reasons: _reasons, notificationOptions: _notificationOptions, ), ); } Future<void> _onAuthenticationLoggedOut(AuthenticationLoggedOut event, Emitter<AuthenticationState> emit) async { await authenticationRepository.deleteToken(); await Future<dynamic>.delayed(const Duration(seconds: 2)); emit(AuthenticationUnauthenticated()); add(AuthenticationStarted()); } }

Authstate.dart Authstate.dart

 part of 'authentication_bloc.dart'; abstract class AuthenticationEvent extends Equatable { const AuthenticationEvent(); @override List<Object> get props => <Object>[]; } class AuthenticationStarted extends AuthenticationEvent {} class AuthenticationLoggedIn extends AuthenticationEvent { final String token; const AuthenticationLoggedIn({required this.token}); @override List<Object> get props => <Object>[token]; } class AuthenticationLoggedOut extends AuthenticationEvent {}

AuthEvent.dart AuthEvent.dart

 part of 'authentication_bloc.dart'; abstract class AuthenticationState extends Equatable { const AuthenticationState(); @override List<Object> get props => <Object>[]; } class AuthenticationInitial extends AuthenticationState {} class AuthenticationUnauthenticated extends AuthenticationState {} class AuthenticationLoadSuccess extends AuthenticationState { final SysConfig sysConfig; final Settings settings; final CountriesModelList countries; final ReasonsModelList reasons; final NotificationOptionsList notificationOptions; const AuthenticationLoadSuccess({required this.sysConfig, required this.settings, required this.countries, required this.reasons, required this.notificationOptions}); @override List<Object> get props => <Object>[sysConfig, settings, countries, reasons, notificationOptions]; } class AuthenticationLoadInProgress extends AuthenticationState {} class AuthenticationLoadFailure extends AuthenticationState { final MYException exception; const AuthenticationLoadFailure({required this.exception}); @override List<Object> get props => <Object>[exception]; }

you have to change a lot of thinks.你必须改变很多想法。 First of all you need to add the repository/ies to your bloc constructor to inject the mocks.首先,您需要将存储库/ies 添加到您的 bloc 构造函数以注入模拟。

 class AuthenticationBloc extends Bloc<AuthenticationEvent, AuthenticationState> { late final AuthenticationRepository authenticationRepository; final SettingsRepository _settingsRepository = SettingsRepository(); AuthenticationBloc({required this.authenticationRepository}): super(AuthenticationInitial()) { // Register events here on<AuthenticationStarted>(_onAuthenticationStarted); on<AuthenticationLoggedIn>(_onAuthenticationLoggedIn); on<AuthenticationLoggedOut>(_onAuthenticationLoggedOut); }

Then you can use the mock when creating the bloc in the setup method然后你可以在 setup 方法中创建 bloc 时使用 mock

 setUp(() { authenticationRepositoryMock = MockWeatherRepository(); authenticationBloc = AuthenticationBloc(authenticationRepository: authenticationRepositoryMock ); });

Then you have to return that bloc in the build function of your blocTest and also you have to setup the mock behavior there然后你必须在你的 blocTest 的构建 function 中返回那个 bloc 并且你必须在那里设置模拟行为

build: () { when(() => authenticationRepositoryMock.hasToken()).thenAnswer((_) async => true); return bloc; },

Then add an event to your bloc in the act function然后在 function 行为中向您的集团添加一个事件

act: (dynamic b) => b.add(AuthenticationStarted()),

And then you can check the result in the expect function.然后你可以在期望的 function 中检查结果。 (i think the initial state will not be emitted here) (我认为最初的 state 不会在这里发出)

 expect: () => [ AuthenticationLoadSuccess(...),

It also a good idea to mock the SettingsRepository.模拟 SettingsRepository 也是一个好主意。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM