am使用json(反)序列化手动设置模型类。到目前为止,我已经实现了一些测试用例。特别是我要检查toJson-> fromJson是否是我的模型类型(称为会话)的标识。
这是我的模型类型的相关部分:
class Session extends Equatable {
final List<User> audience;
/* ... */
Session.fromJson(Map<String, dynamic> json) :
/* ... */
audience = (json['audience'] as List).map(((it) =>
User.fromMap(it))).toList();
Map<String, dynamic> toJson() => {
/* ... */
'audience': audience.map((it) => it.toJson()).toList()
};
}
这些是audience
字段中的类型:
class User extends Equatable {
factory User.fromJson(Map<String, dynamic> json) {
if (_isRegisteredUserJson(json)) {
return RegisteredUser.fromMap(json);
} else {
return User(id: json['id']);
}
}
/* ... */
}
class RegisteredUser extends User {/* ... */}
在测试中,我像这样设置了受众字段(使用faker库):
User _user() => User(id: faker.guid.guid());
RegisteredUser _registeredUser() => RegisteredUser(
id: faker.guid.guid(),
alias: faker.person.name(),
email: faker.internet.email());
Session _session => Session(
audience: faker.randomGenerator
.amount((n) => n % 3 == 0 ? _registeredUser() : _user, 100)
.cast<User>()
/* ... */
);
我希望audience
返回后,User
列表中仅包含类型为RegisteredUser
或toJson()
的元素。取而代之的是,我得到一个包含RegisteredUsers
或_Closure: () => 'User from Function'
的列表,但我不确定这是什么。
结果,我得到以下错误消息进行测试:
00:00 +4 -1: toJson -> fromJson is identity for Session [E]
type '() => User' is not a subtype of type 'User' in type cast
dart:_internal/cast.dart 99:46 _CastListBase.[]
dart:collection/list.dart 60:33 __CastListBase&_CastIterableBase&ListMixin.elementAt
dart:_internal/iterable.dart 414:40 MappedListIterable.elementAt
dart:_internal/iterable.dart 219:19 ListIterable.toList
package:feedback/model/base_module.dart 42:54 BaseModule.toJson
package:feedback/model/session.dart 51:23 Session.toJson
test/json_test.dart 34:47 main.<fn>.<fn>
package:test_api/src/backend/declarer.dart 168:27 Declarer.test.<fn>.<fn>.<fn>
===== asynchronous gap ===========================
dart:async/future_impl.dart 22:43 _Completer.completeError
dart:async/runtime/libasync_patch.dart 40:18 _AsyncAwaitCompleter.completeError
package:test_api/src/backend/declarer.dart Declarer.test.<fn>.<fn>.<fn>
===== asynchronous gap ===========================
dart:async/zone.dart 1053:19 _CustomZone.registerUnaryCallback
dart:async/runtime/libasync_patch.dart 77:23 _asyncThenWrapperHelper
package:test_api/src/backend/declarer.dart Declarer.test.<fn>.<fn>.<fn>
package:test_api/src/backend/invoker.dart 250:15 Invoker.waitForOutstandingCallbacks.<fn>
===== asynchronous gap ===========================
dart:async/zone.dart 1045:19 _CustomZone.registerCallback
dart:async/zone.dart 962:22 _CustomZone.bindCallbackGuarded
dart:async/timer.dart 52:45 new Timer
dart:async/timer.dart 87:9 Timer.run
dart:async/future.dart 174:11 new Future
package:test_api/src/backend/invoker.dart 399:21 Invoker._onRun.<fn>.<fn>.<fn>
00:00 +4 -1: Some tests failed.
Unhandled exception:
Dummy exception to set exit code.
#0 _rootHandleUncaughtError.<anonymous closure> (dart:async/zone.dart:1112:29)
#1 _microtaskLoop (dart:async/schedule_microtask.dart:41:21)
#2 _startMicrotaskLoop (dart:async/schedule_microtask.dart:50:5)
#3 _Timer._runTimers (dart:isolate/runtime/libtimer_impl.dart:391:30)
#4 _Timer._handleMessage (dart:isolate/runtime/libtimer_impl.dart:416:5)
#5 _RawReceivePortImpl._handleMessage (dart:isolate/runtime/libisolate_patch.dart:171:12)
答案 0 :(得分:0)
感谢乔丹·戴维斯,这是答案:
在方法调用_user
中,我不小心省略了方括号,因此在需要该函数结果的地方传递了一个函数作为参数。