我正在尝试编写测试以调试我的应用。我想尝试登录页面,我有两个文本字段和一个按钮。当文本字段被填充时,如果我点击按钮,我有一个Dio请求登录,如果可以的话,导航到HomePage。我有以下测试:
testWidgets('Login ok',
(WidgetTester tester) async {
LoginPage loginPage = new LoginPage();
final mockObserver = MockNavigatorObserver();
await tester.pumpWidget(
MaterialApp(
home: loginPage,
navigatorObservers: [mockObserver],
),
);
var app = new MediaQuery(
data: new MediaQueryData(), child: new MaterialApp(home: loginPage));
await tester.pumpWidget(app);
Finder username = find.byKey(new Key('emailField'));
Finder password = find.byKey(new Key('passwordField'));
await tester.enterText(username, "user");
await tester.enterText(password, "pass");
await tester.pump();
Finder formWidgetFinder = find.byType(Form);
Form formWidget = tester.widget(formWidgetFinder) as Form;
GlobalKey<FormState> formKey = formWidget.key as GlobalKey<FormState>;
expect(formKey.currentState.validate(), isTrue);
Finder loginButon = find.byKey(new Key('loginButon'));
await tester.tap(loginButon);
await tester.pumpAndSettle();
verify(mockObserver.didPush(any, any));
expect(find.byType(HomePage), findsOneWidget);
});
但是我认为
verify(mockObserver.didPush(any,any));
不检查应用是否转到新页面,因为Dio请求由于以下错误而失败:
DioError [DioErrorType.DEFAULT]: type '_StreamHandlerTransformer<List<int>, Uint8List>' is not a subtype of type 'StreamTransformer<Uint8List, Uint8List>'#0 _MockHttpResponse.transform (package:flutter_test/src/binding.dart:2012:35)
#1 DefaultHttpClientAdapter.fetch (package:dio/src/adapter.dart:165:46)
<asynchronous suspension>
#2 Dio._makeRequest (package:dio/src/dio.dart:739:46)
<asynchronous suspension>
...
我尝试了许多版本的Dio,2.0.1、2.0.9、2.0.10、2.0.13,但没有任何效果。如果我在模拟器上运行我的应用,那么Dio请求就可以了。
我该如何解决?还是有另一种方法可以在Flutter上测试导航?