颤动冻结:等于对象类型不相同

flutter freezed : equals type of object is not same

我正在使用 freezed 从 json 制作对象:

@freezed
class UserMessagesResponseModel with _$UserMessagesResponseModel {
  const factory UserMessagesResponseModel({
    final Data? data,
  }) = _UserMessagesResponseModel;

  factory UserMessagesResponseModel.fromJson(final Map<String, dynamic> json) =>
      _$UserMessagesResponseModelFromJson(json);
}

@freezed
class Data with _$Data {
  const factory Data({
    final Messages? messages,
  }) = _Data;

  factory Data.fromJson(final Map<String, dynamic> json) =>
      _$DataFromJson(json);
}...

现在我正在尝试对创建的对象进行测试和检查类型:

test('should return a valid model', () async {
  final jsonMap =
      json.decode(fixture('message.json')) as Map<String, dynamic>;

  final result = UserMessagesResponseModel.fromJson(
    jsonMap,
  );
    
  expect(result, equals(UserMessagesResponseModel));
});

为什么我得到错误:

Expected: Type:<UserMessagesResponseModel>
  Actual: _$_UserMessagesResponseModel:<UserMessagesResponseModel...

冷冻 不是同款?如何检查类型?

我还用过:

expect(result, isA<UserMessagesResponseModel>);

但我得到了这个:

Expected: <Closure: () => TypeMatcher<UserMessagesResponseModel> from Function 'isA': static.>
  Actual: _$_UserMessagesResponseModel:<UserMessagesResponseModel

第一个例子

expect(result, equals(UserMessagesResponseModel));

不起作用,因为结果是一个实例而 UserMessagesResponseModel 是一种类型,您应该使用 isA.

但是在第二个例子中我使用了isA,它仍然不起作用!

您错过了 isA 之后的 (),我相信这就是它不起作用的原因。

来自这个:

expect(result, isA<UserMessagesResponseModel>);

对此:

expect(result, isA<UserMessagesResponseModel>());