我正在使用mockito来模拟服务器调用并对服务器调用进行存根以返回成功响应的未来.问题是当我调用pumpAndSettle()时,它正在超时,因为它没有等待将来完成并返回成功响应.
如果我不是很清楚,我道歉,但这里是示例代码:
login_bloc.dart
class LoginBloc { LoginRepository _loginRepository; final String searchKeyword = "special-keyword"; final _urlString = PublishSubject<String>(); final _isLoggedIn = BehaviorSubject<bool>(seedValue: false); final _errorMessage = PublishSubject<String>(); Observable<bool> get isLoggedIn => _isLoggedIn.stream; Observable<String> get isErrorState => _errorMessage.stream; LoginBloc({LoginRepository loginRepository}) : _loginRepository = loginRepository ?? LoginRepository() { // Listen on the _urlString stream to call the function which checks for the special keyword and if a match is found make a server call _urlString.stream.Listen((String url) { _authorizationFullService(url); }); } // Search for special keyword and if a match is found call the server call function voID _authorizationFullService(String url) { if (url.contains(searchKeyword)) { int index = url.indexOf(searchKeyword); String result = url.substring(index + searchKeyword.length); result = result.trim(); String decodedUrl = Uri.decodeFull(result); if (decodedUrl != null && decodedUrl.length > 0) { _fullServiceServerCall(decodedUrl); } else { _isLoggedIn.sink.add(false); } } } // Call server call function from repository which returns a future of the Authorization object voID _fullServiceServerCall(String decodedUrl) { _loginRepository .getSession(decodedUrl) .then(_handleSuccessAuthorization) .catchError(_handleErrorState); } // Handle success response and set the login stream voID _handleSuccessAuthorization(Authorization authorization) { if (authorization != null && authorization.IDnumber != 0) { _isLoggedIn.sink.add(true); } else { _isLoggedIn.sink.add(false); } } // Handle error response and set the error stream voID _handleErrorState(dynamic error) { _isLoggedIn.sink.add(false); _errorMessage.sink.add(error.toString()); } voID dispose() { _urlString.close(); _isLoggedIn.close(); _errorMessage.close(); }}
Widget_test.dart
group('Full Login Navigation test',() { LoginRepository mockLoginRepository; LoginBloc loginBloc; NotificationBloc notificationBloc; NavigatorObserver mockNavigatorObserver; Authorization _auth; String testUrl; setUp(() { mockLoginRepository = MockLoginRepository(); _auth = Authorization((auth) => auth ..param1 = "foo" ..param2 = "bar" ..param3 = "foobar" ..param4 = "barfoo"); loginBloc = LoginBloc(loginRepository: mockLoginRepository); mockNavigatorObserver = MockNavigatorObserver(); testUrl = "http://test.test.com"; }); Future<Null> _buildFullLoginPage(LoginBloc loginBloc,NotificationBloc notificationBloc,WidgetTester tester) async { when(mockLoginRepository.getSession(testUrl)) .thenAnswer((_) => Future.value(_auth)); await tester.pumpWidget(LoginBlocProvIDer( child: NotificationBlocProvIDer( child: MaterialApp( home: LoginFullService(),onGenerateRoute: NavigationRoutes.routes,navigatorObservers: [mockNavigatorObserver],),notificationBloc: notificationBloc,loginBloc: loginBloc,)); //Todo: Remove casting to dynamic after dart sdk BUG fix: https://github.com/dart-lang/mockito/issues/163 verify(mockNavigatorObserver.dIDPush(any,any) as dynamic); loginBloc.getAuthorization( "http://testing.testing.com?search-keyword=http%3A%2F%2Ftest.test.com"); } testWidgets('Navigate to landing page on correct login url',(WidgetTester tester) async { await _buildFullLoginPage(loginBloc,notificationBloc,tester); await tester.pumpAndSettle(); expect(find.byKey(Key('webvIEw_scaffold')),findsnothing); //Todo: Remove casting to dynamic after dart sdk BUG fix: https://github.com/dart-lang/mockito/issues/163 verify(mockNavigatorObserver.dIDPush(any,any) as dynamic); });});
在运行小部件测试时,testWidgets中的tester.pumpAndSettle()在未来完成之前超时.这是错误日志:
══╡ EXCEPTION CAUGHT BY FlutteR TEST FRAMEWORK ╞════════════════════════════════════════════════════The following assertion was thrown running a test:pumpAndSettle timed outWhen the exception was thrown,this was the stack:#0 WidgetTester.pumpAndSettle.<anonymous closure> (package:Flutter_test/src/Widget_tester.dart:299:11)<asynchronous suspension>#3 TestAsyncUtils.guard (package:Flutter_test/src/test_async_utils.dart:69:41)#4 WidgetTester.pumpAndSettle (package:Flutter_test/src/Widget_tester.dart:295:27)#5 main.<anonymous closure>.<anonymous closure> (file:///Users/ssIDdh/documents/projects/mobile-Flutter/test/ui/pages/login/login_full_test.dart:114:20)<asynchronous suspension>#6 testWidgets.<anonymous closure>.<anonymous closure> (package:Flutter_test/src/Widget_tester.dart:72:23)#7 TestWidgetsFlutterBinding._runTestbody (package:Flutter_test/src/binding.dart:555:19)<asynchronous suspension>#10 TestWidgetsFlutterBinding._runTest (package:Flutter_test/src/binding.dart:539:14)#11 automatedTestWidgetsFlutterBinding.runTest.<anonymous closure> (package:Flutter_test/src/binding.dart:883:24)#17 automatedTestWidgetsFlutterBinding.runTest (package:Flutter_test/src/binding.dart:880:15)#18 testWidgets.<anonymous closure> (package:Flutter_test/src/Widget_tester.dart:71:22)#19 Declarer.test.<anonymous closure>.<anonymous closure>.<anonymous closure> (package:test/src/backend/declarer.dart:168:27)<asynchronous suspension>#20 Invoker.waitForOutstandingCallbacks.<anonymous closure> (package:test/src/backend/invoker.dart:249:15)<asynchronous suspension>#25 Invoker.waitForOutstandingCallbacks (package:test/src/backend/invoker.dart:246:5)#26 Declarer.test.<anonymous closure>.<anonymous closure> (package:test/src/backend/declarer.dart:166:33)#31 Declarer.test.<anonymous closure> (package:test/src/backend/declarer.dart:165:13)<asynchronous suspension>#32 Invoker._onRun.<anonymous closure>.<anonymous closure>.<anonymous closure>.<anonymous closure> (package:test/src/backend/invoker.dart:403:25)<asynchronous suspension>#46 _Timer._runTimers (dart:isolate/runtime/libtimer_impl.dart:382:19)#47 _Timer._handleMessage (dart:isolate/runtime/libtimer_impl.dart:416:5)#48 _RawReceivePortImpl._handleMessage (dart:isolate/runtime/libisolate_patch.dart:169:12)(elIDed 30 frames from class _FakeAsync,package dart:async,and package stack_trace)
我真的很感激任何帮助或反馈.
解决方法@H_301_35@ 尝试用你的测试包装testWidgets('Navigate to landing page on correct login url',(WidgetTester tester) async { await tester.runAsync(() async { // test code here }); });总结
以上是内存溢出为你收集整理的dart – 如何在Flutter小部件测试中等待未来完成?全部内容,希望文章能够帮你解决dart – 如何在Flutter小部件测试中等待未来完成?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)