Home > Blockchain >  How to write a unit test for a converter?
How to write a unit test for a converter?

Time:06-11

I am writing a unit test for a converter. My converter has two functions: convert the local model to the database model and vice versa. The converter takes the model of the group that comes. How to write a unit test for a converter? Test:

    main() {
      group('Converter', () {
        test('The result should be a database model converted to a local model',
            () {
          final groupConverter = GroupDbConverter();
          expect();
        });
        test('The result should be a local model converted to a database model',
            () {
          final groupConverter = GroupDbConverter();
          expect();
        });
      });
    }

Converter:

    class GroupDbConverter extends BaseConverter<GroupDBData, Group> {
      @override
      Group processConvertInToOut(GroupDBData inObject) {
        return Group(
          id: inObject.id,
          groupName: inObject.groupName,
        );
      }

      @override
      GroupDBData processConvertOutToIn(Group outObject) {
        return GroupDBData(
          id: outObject.id,
          groupName: outObject.groupName ?? '',
        );
      }
    }

CodePudding user response:

You'll need to construct two objects - The input object, and an object which matches the result that you want to get. Then, run the conversion method on the input object, and check that it matches your expected result.

One caveat: in order for the expect function to properly compare the two objects for equality, you'll need to either override the == operator for Group and GroupDBData, or use the equatable package (which does the same thing with less hassle).

For example (guessing at some of the details):

// group_db_data.dart

class GroupDBData extends Equatable {
  final int id;
  final String groupName;

  GroupDBData({
    required this.id,
    required this.groupName,
  });

  @override
  List<Object> get props => [id, groupName];
}
// group.dart

class Group extends Equatable {
  final int id;
  final String groupName;

  Group({
    required this.id,
    required this.groupName,
  });

  @override
  List<Object> get props => [id, groupName];
}

Finally, to perform the tests:

main() {
  group('Converter', () {
    test('The result should be a database model converted to a local model',
        () {
      final groupConverter = GroupDbConverter();
      final model = GroupDBData(id: 132, groupName: "Test Group");
      final expected = Group(id: 132, groupName: "Test Group");

      final actual = groupConverter.processConvertInToOut(model);

      expect(actual, expected);
    });

    test('The result should be a local model converted to a database model',
        () {
      final groupConverter = GroupDbConverter();
      final group = Group(id: 132, groupName: "Test Group");
      final expected = GroupDBData(id: 132, groupName: "Test Group");

      final actual = groupConverter.processConvertOutToIn(group);

      expect(actual, expected);
    });
  });
}
  • Related