I'm stuck with a unit test and fairly new to testing. I tried to create a test for a class with toMap/fromMap methods and write tests for it.
Within the class I have the following code, where I added the hashCode and == operator methods to prepare the class for the test.
I have the exact same setup for other classes, where the test works..
String uid;
String email;
String userName;
String diagnose;
bool surgery;
List<Goal> goals;
List<KOS> kos;
UserCase({
this.uid,
this.email,
this.userName,
this.diagnose,
this.surgery = false,
this.goals,
this.kos,
});
factory UserCase.fromData(Map<String, dynamic> data) {
if (data == null) {
print('UserCase fromData NULL');
return null;
}
final String uid = data['uid'] ?? '';
final String email = data['email'] ?? '';
final String userName = data['userName'] ?? '';
final String diagnose = data['diagnose'] ?? '';
final bool surgery = data['surgery'] ?? false;
final List<Goal> goals = data['goals'] == null
? []
: List.from(data['goals'].map((e) => Goal.fromData(e)));
final List<KOS> kos = data['kos'] == null
? []
: List.from(data['kos'].map((e) => KOS.fromData(e)));
return UserCase(
uid: uid,
email: email,
userName: userName,
diagnose: diagnose,
surgery: surgery,
goals: goals,
kos: kos,
);
}
Map<String, dynamic> toMap() {
return {
'uid': uid,
'email': email,
'userName': userName,
'diagnose': diagnose,
'surgery': surgery,
'goals': goals == null || goals.isEmpty
? []
: goals.map((e) => e.toMap()).toList(),
'kos':
kos == null || kos.isEmpty ? [] : kos.map((e) => e.toMap()).toList(),
};
}
@override
int get hashCode {
return hashValues(uid, email, userName, diagnose, surgery, goals, kos);
}
@override
bool operator ==(other) {
if (identical(this, other)) return true;
if (runtimeType != other.runtimeType) return false;
final UserCase otherCase = other;
return uid == otherCase.uid &&
email == otherCase.email &&
userName == otherCase.userName &&
diagnose == otherCase.diagnose &&
surgery == otherCase.surgery &&
goals == otherCase.goals &&
kos == otherCase.kos;
}
}
And this is the test that fails:
final userCase = UserCase.fromData({
'uid': 'id123',
'email': 'email123',
'userName': 'username',
'diagnose': 'ACL',
'surgery': true,
'goals': [],
'kos': [],
});
expect(
userCase,
UserCase(
uid: 'id123',
email: 'email123',
userName: 'username',
diagnose: 'ACL',
surgery: true,
goals: [],
kos: [],
));
});
And that is the error message:
.../test/usercase_test.dart: fromData case with all properties [E]
'dart:ui/hash_codes.dart': Failed assertion: line 17: '<optimized out>': is not true.
dart:ui hashValues
../lib/Classes/UserCase.dart 71:12 UserCase.hashCode
dart:collection _CompactLinkedHashSet.contains
package:matcher/src/pretty_print.dart 28:14 prettyPrint._prettyPrint
package:matcher/src/pretty_print.dart 119:22 prettyPrint
package:matcher/src/description.dart 49:11 StringDescription.addDescriptionOf
package:matcher/src/equals_matcher.dart 267:19 _DeepMatcher.describe
package:matcher/src/description.dart 47:13 StringDescription.addDescriptionOf
package:test_api expect
package:flutter_test/src/widget_tester.dart 431:3 expect
usercase_test.dart 22:7 main.<fn>.<fn>´´´
Aucun commentaire:
Enregistrer un commentaire