본문 바로가기
flutter

flutter unit test 구현

by Rogan_Kim 2023. 9. 4.
728x90

프로젝트가 점점 거대해지면서 다 확인하기 힘들어지면서 테스트코드의 중요성을 느끼고

프론트, 백엔드 상관없이 작성하는 습관을 기르고 있다.

그 중 flutter unit test 예시를 기록해 보았다.

 

 

테스트 대상 Model

enum UserType { parent, child, other }

class UserModel {
  int? id;
  int? kakaoId;
  String? phoneNumber;
  String? email;
  String? nickname;
  String? profileImage;
  String? profileStatusMessage;
  String? region;
  String? birthyear;
  UserType? userType;
  Gender? gender;

  UserModel({
    this.id,
    this.kakaoId,
    this.phoneNumber,
    this.email,
    this.nickname,
    this.profileImage,
    this.profileStatusMessage,
    this.region,
    this.birthyear,
    this.gender,
    this.userType,
  });

  factory UserModel.fromJson(Map<String, dynamic> json) {
    return UserModel(
      id: json['id'],
      kakaoId: json['kakaoId'],
      phoneNumber: json['phoneNumber'],
      email: json['email'],
      gender: json['gender'] != null
          ? Gender.values.firstWhere((element) {
              return element.toString() == 'Gender.${json['gender']}';
            })
          : null, 
      birthyear: json['birthyear'],
      userType: json['userType'] != null
          ? UserType.values.firstWhere((element) {
              return element.toString() == 'UserType.${json['userType']}';
            })
          : null,
      nickname: json['nickname'],
      profileImage: json['profileImage'],
      profileStatusMessage: json['profileStatusMessage'],
      region: json['region'],
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'email': email,
      'kakaoId': kakaoId,
      'nickname': nickname,
      'phoneNumber': phoneNumber,
      'profileImage': profileImage,
      'profileStatusMessage': profileStatusMessage,
      'region': region,
      'birthyear': birthyear,
      'gender': gender?.name ?? Gender.other.name,
      'userType': userType,
    };
  }

  @override
  String toString() => 'UserModel{id: $id,'
      'kakaoId: $kakaoId,'
      'phoneNumber: $phoneNumber,'
      'email: $email,'
      'nickname: $nickname,'
      'profileImage: $profileImage,'
      'profilesStatusMessage: $profileStatusMessage,'
      'region: $region,'
      'birthyear: $birthyear,'
      'userType: $userType,'
      'Gender: $gender }';
}

 

 

 

1. 테스트 코드 위치

테스트 코드는 project를 생성하면 있는 test 폴더 하위에 'fileName_test.dart' 형식에 맞춰서 저장한다.

아래를 참고.

 

 

2. 테스트 코드 작성

void main() {
  test('UserModel Test', () {
    final user = UserModel(
      id: 1,
      kakaoId: 25555,
      phoneNumber: "01099999999",
      email: "test@test.com",
      nickname: "tester",
      profileImage: "profileImage",
      profileStatusMessage: "profileStatusMessage",
      region: "대한민국",
      birthyear: "1997",
      userType: UserType.other,
      gender: Gender.male,
    );
    expect(user.nickname, "tester");
  });

  test(".fromJson Constructor", () {
    final user = UserModel.fromJson({
      "id": 1,
      "kakaoId": 25555,
      "phoneNumber": "01099999999",
      "email": "test@test.com",
      "nickname": "tester",
      "profileImage": "profileImage",
      "profileStatusMessage": "profileStatusMessage",
      "region": "대한민국",
      "birthyear": "1997",
      "userType": "other",
      "gender": "male",
    });

    expect(user.nickname, "tester");
    expect(user.userType, UserType.other);
    expect(user.gender, Gender.male);
  });

  test("toJson Method", () {
    final user = UserModel(
      id: 1,
      kakaoId: 25555,
      phoneNumber: "01099999999",
      email: "test@test.com",
      nickname: "tester",
      profileImage: "profileImage",
      profileStatusMessage: "profileStatusMessage",
      region: "대한민국",
      birthyear: "1997",
      userType: UserType.other,
      gender: Gender.male,
    );
    final json = user.toJson();
    expect(json['nickname'], "tester");
  });
}

 

'test()' 함수 내부에 final로 user를 중복 생성한거를 볼 수 있는데 이를 피할려고 main아래에다가 선언해서 test를 작성 해보았다.

 

void main() {
  final user = UserModel(
    id: 1,
    kakaoId: 25555,
    phoneNumber: "01099999999",
    email: "test@test.com",
    nickname: "tester",
    profileImage: "profileImage",
    profileStatusMessage: "profileStatusMessage",
    region: "대한민국",
    birthyear: "1997",
    userType: UserType.other,
    gender: Gender.male,
  );

  final fromJsonUser = UserModel.fromJson({
    "id": 1,
    "kakaoId": 25555,
    "phoneNumber": "01099999999",
    "email": "test@test.com",
    "nickname": "tester",
    "profileImage": "profileImage",
    "profileStatusMessage": "profileStatusMessage",
    "region": "대한민국",
    "birthyear": "1997",
    "userType": "other",
    "gender": "male",
  });

  final jsonUser = user.toJson();

  test('UserModel Test', () {
    expect(user.nickname, "tester");
  });

  test(".fromJson Constructor", () {
    expect(fromJsonUser.nickname, "tester");
    expect(fromJsonUser.userType, UserType.other);
    expect(fromJsonUser.gender, Gender.male);
  });

  test("toJson Method", () {
    expect(jsonUser['nickname'], "tester");
  });
}

 

 

 

728x90

댓글