使用提供者状态管理测试小部件

时间:2020-08-25 14:38:45

标签: flutter testing dart provider widget-test-flutter

Flutter Doctor -v结果:

 C:\Users\daydreamer>flutter doctor -v
 [√] Flutter (Channel stable, 1.20.2, on Microsoft Windows [Version 10.0.18363.1016], locale tr-TR)
• Flutter version 1.20.2 at C:\src\flutter\flutter_windows_v1.5.4-hotfix.2-stable\flutter
• Framework revision bbfbf1770c (12 days ago), 2020-08-13 08:33:09 -0700
• Engine revision 9d5b21729f
• Dart version 2.9.1


 [√] Android toolchain - develop for Android devices (Android SDK version 30.0.1)
• Android SDK at C:\Users\daydreamer\AppData\Local\Android\sdk
• Platform android-30, build-tools 30.0.1
• Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
• Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)
• All Android licenses accepted.

 [√] Android Studio (version 4.0)
• Android Studio at C:\Program Files\Android\Android Studio
• Flutter plugin version 48.1.2
• Dart plugin version 193.7361
• Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)

 [√] VS Code (version 1.48.1)
• VS Code at C:\Users\daydreamer\AppData\Local\Programs\Microsoft VS Code
• Flutter extension version 3.13.2

 [!] Connected device
 ! No devices available

我正在使用提供程序包对我的应用程序进行状态管理。我想通过测试来测试我的登录屏幕。我的代码:

  Widget buildTestableWidget(Widget widget) {
  return MediaQuery(data: MediaQueryData(), child: MaterialApp(home: widget));}

 testWidgets('Try to login with email and password',(WidgetTester tester) async {
     await tester.pumpWidget(
       MultiProvider(
         providers: [
           ChangeNotifierProvider(create: (context) => TicketUserViewModel()),
         ],
         builder: (context, child) => buildTestableWidget(LoginPage()),
       ),
     );

     final emailTextFormField = find.byKey(Key('email'));
     expect(emailTextFormField, findsOneWidget);

     final passwordTextFormField = find.byKey(Key('password'));
     expect(passwordTextFormField, findsOneWidget);

     final loginButton = find.byKey(Key('login'));
     expect(loginButton, findsOneWidget);

     await tester.enterText(emailTextFormField, "testmail");
     await tester.enterText(passwordTextFormField, "testpassword");
   });

即使艰难,我仍然没有向api发出http请求,但出现很多错误。我到目前为止尝试的是,修复flutter pub缓存,切换到主通道,尝试仅运行代码ChangeNotifierProvider而不使用多提供者,flutter clean。我通常会编写简单的测试来在屏幕上查找小部件,并且可以正常工作:

错误:

lib/core/viewmodels/ticket_user_view_model.dart:47:60: Error: The argument type 'TicketUserLogin/*1*/' can't be assigned to the parameter type 'TicketUserLogin/*2*/'. -     'TicketUserLogin/*1*/' is from 'lib/core/models/ticket_tracking_models/user_login.dart'. - 'TicketUserLogin/*2*/' is from 'package:samples/core/models/ticket_tracking_models/user_login.dart' ('lib/core/models/ticket_tracking_models/user_login.dart'). _currentUser = await NodeJsApiService.instance.login(user); ^ lib/core/viewmodels/ticket_user_view_model.dart:47:22: Error: A value of type 'TicketUser/*1*/' can't be assigned to a variable of type 'TicketUser/*2*/'. - 'TicketUser/*1*/' is from 'package:samples/core/models/ticket_tracking_models/user_model.dart' ('lib/core/models/ticket_tracking_models/user_model.dart'). - 'TicketUser/*2*/' is from 'lib/core/models/ticket_tracking_models/user_model.dart'. _currentUser = await NodeJsApiService.instance.login(user); ^ lib/core/viewmodels/ticket_user_view_model.dart:58:21: Error: A value of type 'List<ListSoldTickets/*1*/>' can't be assigned to a variable of type 'List<ListSoldTickets/*2*/>'. - 'List' is from 'dart:core'. - 'ListSoldTickets/*1*/' is from 'package:samples/core/models/ticket_tracking_models/list_sold_tickets.dart' ('lib/core/models/ticket_tracking_models/list_sold_tickets.dart'). - 'ListSoldTickets/*2*/' is from 'lib/core/models/ticket_tracking_models/list_sold_tickets.dart'. soldTickets = await NodeJsApiService.instance.listSoldTickets(userId); ^ lib/core/viewmodels/ticket_user_view_model.dart:69:71: Error: The argument type 'ForgetPasswordModel/*1*/' can't be assigned to the parameter type 'ForgetPasswordModel/*2*/'. - 'ForgetPasswordModel/*1*/' is from 'lib/core/models/ticket_tracking_models/forget_password.dart'. - 'ForgetPasswordModel/*2*/' is from 'package:samples/core/models/ticket_tracking_models/forget_password.dart' ('lib/core/models/ticket_tracking_models/forget_password.dart'). _commonMessage = await NodeJsApiService.instance.forgetPassword(info); ^ lib/core/viewmodels/ticket_user_view_model.dart:69:24: Error: A value of type 'CommonMessage/*1*/' can't be assigned to a variable of type 'CommonMessage/*2*/'. - 'CommonMessage/*1*/' is from 'package:samples/core/models/ticket_tracking_models/common_message.dart' ('lib/core/models/ticket_tracking_models/common_message.dart'). - 'CommonMessage/*2*/' is from 'lib/core/models/ticket_tracking_models/common_message.dart'. _commonMessage = await NodeJsApiService.instance.forgetPassword(info); ^ lib/core/viewmodels/ticket_user_view_model.dart:80:26: Error: A value of type 'List<AvailableTickets/*1*/>' can't be assigned to a variable of type 'List<AvailableTickets/*2*/>'. - 'List' is from 'dart:core'. - 'AvailableTickets/*1*/' is from 'package:samples/core/models/ticket_tracking_models/available_tickets.dart' ('lib/core/models/ticket_tracking_models/available_tickets.dart'). - 'AvailableTickets/*2*/' is from 'lib/core/models/ticket_tracking_models/available_tickets.dart'. availableTickets = await NodeJsApiService.instance.availableTickets(); ^ lib/core/viewmodels/ticket_user_view_model.dart:91:24: Error: A value of type 'CommonMessage/*1*/' can't be assigned to a variable of type 'CommonMessage/*2*/'. - 'CommonMessage/*1*/' is from 'package:samples/core/models/ticket_tracking_models/common_message.dart' ('lib/core/models/ticket_tracking_models/common_message.dart'). - 'CommonMessage/*2*/' is from 'lib/core/models/ticket_tracking_models/common_message.dart'. _commonMessage = await NodeJsApiService.instance.deleteTicket(ticketId); ^ lib/core/viewmodels/ticket_user_view_model.dart:102:69: Error: The argument type 'BuyTicket/*1*/' can't be assigned to the parameter type 'BuyTicket/*2*/'. - 'BuyTicket/*1*/' is from 'lib/core/models/ticket_tracking_models/buy_ticket.dart'. - 'BuyTicket/*2*/' is from 'package:samples/core/models/ticket_tracking_models/buy_ticket.dart' ('lib/core/models/ticket_tracking_models/buy_ticket.dart'). _commonMessage = await NodeJsApiService.instance.buyNewTicket(ticket); ^ lib/core/viewmodels/ticket_user_view_model.dart:102:24: Error: A value of type 'CommonMessage/*1*/' can't be assigned to a variable of type 'CommonMessage/*2*/'. - 'CommonMessage/*1*/' is from 'package:samples/core/models/ticket_tracking_models/common_message.dart' ('lib/core/models/ticket_tracking_models/common_message.dart'). - 'CommonMessage/*2*/' is from 'lib/core/models/ticket_tracking_models/common_message.dart'. _commonMessage = await NodeJsApiService.instance.buyNewTicket(ticket); ^ lib/core/viewmodels/ticket_user_view_model.dart:113:65: Error: The argument type 'TicketUser/*1*/' can't be assigned to the parameter type 'TicketUser/*2*/'. - 'TicketUser/*1*/' is from 'lib/core/models/ticket_tracking_models/user_model.dart'. - 'TicketUser/*2*/' is from 'package:samples/core/models/ticket_tracking_models/user_model.dart' ('lib/core/models/ticket_tracking_models/user_model.dart'). _currentUser = await NodeJsApiService.instance.userUpdate(user); ^ lib/core/viewmodels/ticket_user_view_model.dart:113:22: Error: A value of type 'TicketUser/*1*/' can't be assigned to a variable of type 'TicketUser/*2*/'. - 'TicketUser/*1*/' is from 'package:samples/core/models/ticket_tracking_models/user_model.dart' ('lib/core/models/ticket_tracking_models/user_model.dart'). - 'TicketUser/*2*/' is from 'lib/core/models/ticket_tracking_models/user_model.dart'. _currentUser = await NodeJsApiService.instance.userUpdate(user); ^ lib/core/viewmodels/ticket_user_view_model.dart:123:72: Error: The argument type 'CreateNewTicket/*1*/' can't be assigned to the parameter type 'CreateNewTicket/*2*/'. - 'CreateNewTicket/*1*/' is from 'lib/core/models/ticket_tracking_models/ticket.dart'. - 'CreateNewTicket/*2*/' is from 'package:samples/core/models/ticket_tracking_models/ticket.dart' ('lib/core/models/ticket_tracking_models/ticket.dart'). _commonMessage = await NodeJsApiService.instance.createNewTicket(ticket); ^ lib/core/viewmodels/ticket_user_view_model.dart:123:24: Error: A value of type 'CommonMessage/*1*/' can't be assigned to a variable of type 'CommonMessage/*2*/'. - 'CommonMessage/*1*/' is from 'package:samples/core/models/ticket_tracking_models/common_message.dart' ('lib/core/models/ticket_tracking_models/common_message.dart'). - 'CommonMessage/*2*/' is from 'lib/core/models/ticket_tracking_models/common_message.dart'. _commonMessage = await NodeJsApiService.instance.createNewTicket(ticket); ^ Failed to load "F:\kadir\YazilimOrnekler\Yazilim\samples\flutter\lib\ui\test\widget_test \widget_test.dart": Compilation failed Test: F:\kadir\YazilimOrnekler\Yazilim\samples\flutter\lib\ui\test\widget_test\widget_test.dart Shell: C:\src\flutter\flutter_windows_v1.5.4-hotfix.2-stable\flutter\bin\cache\artifacts\engine \windows-x64\flutter_tester.exe dart:async/stream_controller.dart 634:44 _StreamController.addError dart:async/stream_controller.dart 904:13 _StreamSinkWrapper.addError package:stream_channel/src/guarantee_channel.dart 144:14 _GuaranteeSink._addError package:stream_channel/src/guarantee_channel.dart 135:5 _GuaranteeSink.addError package:flutter_tools/src/test/flutter_platform.dart 471:27 FlutterPlatform._startTest ===== asynchronous gap =========================== dart:async/zone.dart 1121:19 _CustomZone.registerUnaryCallback dart:async-patch/async_patch.dart 83:23 _asyncThenWrapperHelper package:flutter_tools/src/test/flutter_platform.dart 370:36 FlutterPlatform.loadChannel package:flutter_tools/src/test/flutter_platform.dart 323:46 FlutterPlatform.load package:test_core/src/runner/loader.dart 232:38 Loader.loadFile.<fn> ===== asynchronous gap =========================== dart:async/zone.dart 1121:19 _CustomZone.registerUnaryCallback dart:async-patch/async_patch.dart 83:23 _asyncThenWrapperHelper package:test_core/src/runner/loader.dart Loader.loadFile.<fn> package:test_core/src/runner/load_suite.dart 98:31 new LoadSuite.<fn>.<fn> package:test_core/src/runner/load_suite.dart 108:8 new LoadSuite.<fn> package:test_api/src/backend/invoker.dart 235:15 Invoker.waitForOutstandingCallbacks.<fn> dart:async/zone.dart 1190:13 _rootRun dart:async/zone.dart 1093:19 _CustomZone.run dart:async/zone.dart 1630:10 _runZoned dart:async/zone.dart 1550:10 runZoned package:test_api/src/backend/invoker.dart 232:5 Invoker.waitForOutstandingCallbacks package:test_api/src/backend/invoker.dart 388:17 Invoker._onRun.<fn>.<fn>.<fn> ===== asynchronous gap =========================== dart:async/zone.dart 1121:19 _CustomZone.registerUnaryCallback dart:async-patch/async_patch.dart 83:23 _asyncThenWrapperHelper package:test_api/src/backend/invoker.dart Invoker._onRun.<fn>.<fn>.<fn> dart:async/zone.dart 1190:13 _rootRun dart:async/zone.dart 1093:19 _CustomZone.run dart:async/zone.dart 1630:10 _runZoned dart:async/zone.dart 1550:10 runZoned package:test_api/src/backend/invoker.dart 374:9 Invoker._onRun.<fn>.<fn> dart:async/zone.dart 1190:13 _rootRun dart:async/zone.dart 1093:19 _CustomZone.run dart:async/zone.dart 1630:10 _runZoned dart:async/zone.dart 1550:10 runZoned package:test_api/src/backend/invoker.dart 148:7 Invoker.guard package:test_api/src/backend/invoker.dart 418:15 Invoker._guardIfGuarded package:test_api/src/backend/invoker.dart 373:7 Invoker._onRun.<fn> package:stack_trace/src/chain.dart 102:24 Chain.capture.<fn> dart:async/zone.dart 1190:13 _rootRun dart:async/zone.dart 1093:19 _CustomZone.run dart:async/zone.dart 1630:10 _runZoned dart:async/zone.dart 1550:10 runZoned package:stack_trace/src/chain.dart 100:12 Chain.capture package:test_api/src/backend/invoker.dart 372:11 Invoker._onRun package:test_api/src/backend/live_test_controller.dart 152:11 LiveTestController.run dart:async/future.dart 201:37 new Future.microtask.<fn> dart:async/zone.dart 1182:47 _rootRun dart:async/zone.dart 1093:19 _CustomZone.run dart:async/zone.dart 997:7 _CustomZone.runGuarded dart:async/zone.dart 1037:23 _CustomZone.bindCallbackGuarded.<fn> dart:async/zone.dart 1190:13 _rootRun dart:async/zone.dart 1093:19 _CustomZone.run dart:async/zone.dart 997:7 _CustomZone.runGuarded dart:async/zone.dart 1037:23 _CustomZone.bindCallbackGuarded.<fn> dart:async/schedule_microtask.dart 41:21 _microtaskLoop dart:async/schedule_microtask.dart 50:5 _startMicrotaskLoop dart:isolate-patch/isolate_patch.dart 118:13 _runPendingImmediateCallback dart:isolate-patch/isolate_patch.dart 169:5 _RawReceivePortImpl._handleMessage ✖ loading F:\kadir\YazilimOrnekler\Yazilim\samples\flutter\lib\ui\test\widget_test\widget_test.dart Exited (1)

0 个答案:

没有答案
相关问题