0

I'm trying to write a test to debug my app. I want to try out my login page, I have two textfields and one button. When the textfields are filled, then If i tap the button I have a Dio request to Log In and navigate to HomePage if its ok. I have the following test:

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);
});

But I think that

verify(mockObserver.didPush(any, any));

doesn't check if the app go to new page, because the Dio request fail due to the following error:

     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>
...

I have tried many versions of Dio, 2.0.1, 2.0.9, 2.0.10, 2.0.13 but nothing works. If I run my app on simuator, the Dio request is Ok.

How can I solve this or there is another way to test navigation on Flutter?

SantiSori
  • 381
  • 2
  • 15
  • You might have a misunderstanding concerning what a `verify`on a mock does. It only checks that this method was called and nothing else. If you want to check that your app goes to a new page, you have to test that elsewhere. – second Aug 06 '19 at 09:27
  • @second yes, but the problem is that `verify` pass but there isn't a push because the Dio request failed and its impossible that there is a push. – SantiSori Aug 06 '19 at 09:35
  • @second then, how could I know if there is a push? – SantiSori Aug 06 '19 at 09:44
  • Sorry, looks like i was wrong about the `MockNavigatorObserver` part, according to this answer (https://stackoverflow.com/questions/50704647/how-to-test-navigation-via-navigator-in-flutter) your doing the right thing in general. You example code looks almost the same as the answer from `@IiroKrankka` so you might want to check that out. – second Aug 06 '19 at 11:08
  • @second yes, I took from there but it doesn't work propertly. I'm going to do test with test_driver package of Flutter instead. Thank you anyway! – SantiSori Aug 06 '19 at 11:30
  • Ok. For reference: next time you copy code from somewhere please link the source in your question. It's easier for people to track down your problem that way. – second Aug 06 '19 at 11:33
  • Ok, I'll keep that in mind next time. – SantiSori Aug 06 '19 at 11:37

0 Answers0