DEV Community

Cover image for Testing models in Flutter
Sazardev
Sazardev

Posted on

Testing models in Flutter

Know so many people that do not do testing or do not know what Testing means, even you know that another one do not even know the powerful native testing that Flutter integrate in our projects. So let us find out what flutter_test can do for use, and how can make us be more productive and efficient in our projects.

awesome

Model Testing

It is not too usual to do this type of test, but sometimes we need to check the logic of our models or interact directly with the model, and we feel tired of making a UI only to check if the model is correct.

Let's use a simple model of Userthat is ideal to use from **APIs **model, using factory and toJsonmethod.

class User {
  final String username;
  final String email;
  final String token;

  User({
    required this.username,
    required this.email,
    required this.token,
  });

  factory User.fromJson(Map<String, dynamic> json) {
    return User(
      username: json['username'],
      email: json['email'],
      token: json['token'],
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'username': username,
      'email': email,
      'token': token,
    };
  }

  @override
  String toString() {
    return 'User(username: $username, email: $email, token: $token)';
  }
}
Enter fullscreen mode Exit fullscreen mode

And now, you have this model with the name user.dart, then outside of src go to the folder test, if you don't have it create a new one. Then create a new file named user_test.dart. Then we need a classic main:

void main() {
}
Enter fullscreen mode Exit fullscreen mode

Inside of our main add this new test:

test('User toJson() should return a valid JSON map', () {});
Enter fullscreen mode Exit fullscreen mode

Quite simple, right? This is how we create a function test, it is important to ALWAYS have a clear description of what our test will do. Now we need to add some code in our function.

test('User toJson() should return a valid JSON map', () {
    final user = User(
      username: 'sample_username',
      email: 'sample_email@example.com',
      token: 'sample_token',
    );

    final json = user.toJson();

    expect(json['username'], equals('sample_username'));
    expect(json['email'], equals('sample_email@example.com'));
    expect(json['token'], equals('sample_token'));
  });
Enter fullscreen mode Exit fullscreen mode

As you can see, it is a remarkably simple test that we only check if toJson make the JSON that we want, why? You need to check if all the parameters are correct to send to our API.

And now let's evaluate if a JSON send of our API is valid to create a User object for Flutter:

test('User fromJson() should create a valid User object', () {
    final json = {
      'username': 'sample_username',
      'email': 'sample_email@example.com',
      'token': 'sample_token',
    };

    final user = User.fromJson(json);

    expect(user.username, equals('sample_username'));
    expect(user.email, equals('sample_email@example.com'));
    expect(user.token, equals('sample_token'));
  });
Enter fullscreen mode Exit fullscreen mode

Created the file test, and now?

question

After this, we have the simple task to run in the terminal: flutter test and enjoy the green lights (Or red...) and that is all to do for simple testing in models. In a future post I will explain how to test SQLite models or Hive models, etc. But for now, this is all, thanks for reading!

thanks

Top comments (0)