0

here is my model class for a response from the server

@JsonSerializable(createToJson: false)
@immutable
class Miner extends Equatable {
  final String minerId;
  @JsonKey(name: 'name')
  final String minerName;
  @JsonKey(fromJson: _isOwnerToJson)
  final bool isOwner;
  final List<RoleList> roleList;
  final String? withdrawAddress;
  final String? withdrawMinAmount;
  final String ownerUsername;
  final String? rangerCoinType;
  @JsonKey(fromJson: _hashRateFromJson)
  final HashRateStatus hashRate;

  factory Miner.fromJson(Map<String, dynamic> json) => _$MinerFromJson(json);

  static _isOwnerToJson(String isOwner) {
    if (isOwner == '1') {
      return true;
    } else {
      return false;
    }
  }

  static _hashRateFromJson(Map<String, dynamic> hashRate) {
    return HashRateStatus.fromJson(hashRate['BTC']);
  }

  Miner({
    required this.minerId,
    required this.minerName,
    required this.isOwner,
    required this.roleList,
    this.withdrawAddress,
    this.withdrawMinAmount,
    required this.ownerUsername,
    this.rangerCoinType,
    required this.hashRate,
  });

  @override
  List<Object?> get props => [
        minerId,
        minerName,
        isOwner,
        roleList,
        withdrawAddress,
        withdrawMinAmount,
        ownerUsername,
        rangerCoinType,
        hashRate,
      ];
}

@JsonSerializable(createToJson: false)
@immutable
class RoleList extends Equatable {
  @JsonKey(name: 'name')
  final Permission permission;

  RoleList({required this.permission});

  factory RoleList.fromJson(Map<String, dynamic> json) =>
      _$RoleListFromJson(json);

  @override
  List<Object> get props => [permission];
}

enum Permission {
  @JsonValue('owner')
  owner,
  @JsonValue('poolAdmin')
  poolAdmin,
  @JsonValue('workerAdmin')
  workerAdmin,
  @JsonValue('earnAdmin')
  earnAdmin,
  @JsonValue('notificationAdmin')
  notificationAdmin,
}

@JsonSerializable(createToJson: false)
@immutable
class HashRateStatus extends Equatable {
  final String workerCount;
  final String revisedHashRate15m;
  final String revisedHashRate1h;
  final String revisedHashRate1d;
  final String? revisedHashRateLastD;
  @JsonKey(name: 'increase')
  final String? rateOfChange;

  factory HashRateStatus.fromJson(Map<String, dynamic> json) =>
      _$HashRateStatusFromJson(json);

  HashRateStatus({
    required this.workerCount,
    required this.revisedHashRate15m,
    required this.revisedHashRate1h,
    required this.revisedHashRate1d,
    this.revisedHashRateLastD,
    required this.rateOfChange,
  });

  @override
  List<Object?> get props => [
        workerCount,
        revisedHashRate15m,
        revisedHashRate1h,
        revisedHashRate1d,
        revisedHashRateLastD,
        rateOfChange,
      ];
}

and here is my test

test('return list of miner', () async {
        final successfulResponse =
            fixture('sub_account_response_complete.json');
        final miner1 = fixture('miner1.json');
        final miner2 = fixture('miner2.json');
        final response = MockResponse();
        when(() => response.statusCode).thenReturn(200);
        when(() => response.body).thenReturn(successfulResponse);
        when(() => httpClient.get(any()))
            .thenAnswer((_) async => await response);

        final matcher = <Miner>[
          Miner.fromJson(jsonDecode(miner1)),
          Miner.fromJson(jsonDecode(miner2)),
        ];

        final actual = await apiClient.getMinerList(session: session);

        expect(actual, orderedEquals(matcher));
      });

but when I ran the test I've got this error and I can not figure it whats wrong with it.

Expected: equals [
            Miner:Miner(5783, sdfgdfgdfg, true, [RoleList(Permission.owner)], null, 1000000, gidilox581, BTC, HashRateStatus(0, 0, 0, 0, null, null)),
            Miner:Miner(5784, miner123asdasd, true, [RoleList(Permission.owner)], null, 1000000, gidilox581, BTC, HashRateStatus(0, 0, 0, 0, null, null))
          ] ordered
  Actual: [
            Miner:Miner(5783, sdfgdfgdfg, true, [RoleList(Permission.owner)], null, 1000000, gidilox581, BTC, HashRateStatus(0, 0, 0, 0, null, null)),
            Miner:Miner(5784, miner123asdasd, true, [RoleList(Permission.owner)], null, 1000000, gidilox581, BTC, HashRateStatus(0, 0, 0, 0, null, null))
          ]
   Which: at location [0] is Miner:<Miner(5783, sdfgdfgdfg, true, [RoleList(Permission.owner)], 
null, 1000000, gidilox581, BTC, HashRateStatus(0, 0, 0, 0, null, null))> instead of Miner:
<Miner(5783, sdfgdfgdfg, true, [RoleList(Permission.owner)], null, 1000000, gidilox581, BTC, 
HashRateStatus(0, 0, 0, 0, null, null))>


Process finished with exit code 1

Which is wired because I use equatable and I can not find out what is wrong with this code. Any suggestion might be helpful. Thank you in advance.

zex_rectooor
  • 692
  • 7
  • 26

0 Answers0