Home > Software engineering >  Error message when doing unit test in flutter
Error message when doing unit test in flutter

Time:07-11

I am a beginer in flutter and I have to write some unit test for a project but I get an error and can't find the solution to solve it.

Here is the function that I want to test:

  addUser(name, password) async{
    if (name.toString().isEmpty || password.toString().isEmpty) {
      return "Error";
    }
    return await dio.post('https://itsmilife.herokuapp.com/adduser',
    data: {"name": name, "password": password},
    options: Options(contentType: Headers.formUrlEncodedContentType));
  }

Here is the test code:

void main() {
  test('Empty field', () {
    var auth = AuthService();
    String result = auth.addUser("", "");
    expect(result, "Error");
  });
}

And I get this error:

type 'Future<dynamic>' is not a subtype of type 'String'
test\widget_test.dart 17:18  main.<fn>

Can someone help to find where I made a mistake please ? Thanks for yours answers.

CodePudding user response:

There is a missing await in the auth.addUser call and also a missing async in the test function.

It should look like this:

void main() {
  text('Emtpy field', () async {                // <- Here
    var auth = AuthService();
    String result = await auth.addUser("", ""); // <- Here
    expect(result, "Error");
  });
}
  • Related