vendredi 5 octobre 2018

flutter. Mock api call in widget test

I'm trying to create widget test for login screen in application. So i want to test several things: 1. That i can find all needed widgets on screen 2. Empty fields validation 3. Wrong login or password scenario 4. Successfull login scenario

But i stuck with items 3 and 4. As i found out on widget tests flutter doesn't allow api calls - so responce is 400 in every case.

I found a way to write test using mockHtmlClient. Inspired by this topic https://groups.google.com/forum/#!msg/flutter-dev/AnqDqgQ6vus/8BoHfxoNBwAJ

 HttpOverrides.runZoned(() async {
  // All code inside here will use the HttpClient returned below.
  await tester.pumpWidget(buildTestableWidget(LoginScreen()));
  Finder emailField = find.byKey(new Key('email'));
  await tester.enterText(emailField, 'some@some.some');

  Finder passwordField = find.byKey(Key("password"));
  await tester.enterText(passwordField, 'some');



  // tap on the login button

  Finder loginButton = find.byKey(new Key('login'));
  await tester.tap(loginButton);

  // 'pump' the tester again. This causes the widget to rebuild
  await tester.pump();
  //sleep(const Duration(seconds:2));
  // await tester.pump();

  //find validation text on SnackBar
  expect(find.text("Invalid email or password"), findsOneWidget);

}, createHttpClient: createMockHttpClient);

My question is - How can i implement MockHttpClient so i could send correct API responce for 3 and 4 scenarious. I can get json answer from real api on this scenarious. There is implementation of createMockHttpClient for case of NetworkImage in the dicsussion from the linked i posted above.

Aucun commentaire:

Enregistrer un commentaire