Flutter dart tests with http with server callbacks

3,146

You should probably separate both logics.

In the first place you create the test for the UI mocking the response from the server as you say above. This lets you test that the ui flow is the correct depending on the server response.

Andrea Bizotto provides a good example in one of his medium posts.

And later you can test the logic with the server in a separate test. For example, something along these lines.

test('currentUser', () async {
  final Firebase user = await auth.currentUser();
  expect(user, isNotNull);
  expect(user.isAnonymous, isTrue);
  expect(user.isEmailVerified, isFalse);
  .....
});

The example is taken from the firebase plugin tests.

Share:
3,146
pasotee
Author by

pasotee

Unity programmer

Updated on December 07, 2022

Comments

  • pasotee
    pasotee over 1 year

    I am developing some tests for my dart app, but I have some problems with callbacks on button presses.

    For instance, I have a button with a server request callback. When I tap the button with the tester an async suspension is called. I've seen some workarounds for this using mock requests, but I want to perform the actual request to the server. Is there any solution for this.

    Expected result: The tester taps on the button. The button is making the call to the server and then the testing is continued after the request arrived/ after the current state refreshes (any of those would be great).

    If this is not possible, do you have any other suggestions for software to perform this kind of tests? Maybe through Jenkins?

    Code for tapping button:

     testWidgets("Open Login Test", (WidgetTester tester) async{
     await tester.pumpWidget(
        new MaterialApp(
          home: new Material(
            child: new LoginScreen(),
          ),
        ));
    
    expect(find.text("Next"), findsOneWidget);
    expect(find.text("Login"), findsNothing);
    
    Finder emailField = find.byKey(new Key('email'));
    await tester.enterText(emailField, "[email protected]");
    
    var submitButton = find.byKey(new Key('login'));
    expect(submitButton, findsOneWidget);
    
    await tester.tap(submitButton);
    expect(find.text("Next"), findsNothing);
    expect(find.text("Login"), findsOneWidget);
    
    });
    
  • Cícero Moura
    Cícero Moura about 3 years
    Wouldnt it be an integration test? (Using integrationo_test for example).