我需要为 Flutter 项目编写单元测试,如果有一个函数可以遍历同一类型的两个不同对象的所有属性以确保所有值都相同,我会很高兴。
代码示例:
void main() {
test('startLoadingQuizReducer sets isLoading true', () {
var initState = QuizGameState(null, null, null, false);
var expectedState = QuizGameState(null, null, null, true);
var action = StartLoadingQuiz();
var actualState = quizGameReducer(initState, action);
// my test fails here
expect(actualState, expectedState);
});
最佳答案
如何覆盖 ==
进行相等性测试
下面是重写 ==
运算符的示例,以便您可以比较同一类型的两个对象。
class Person {
final String name;
final int age;
const Person({this.name, this.age});
@override
bool operator ==(Object other) =>
identical(this, other) ||
other is Person &&
runtimeType == other.runtimeType &&
name == other.name &&
age == other.age;
@override
int get hashCode => name.hashCode ^ age.hashCode;
}
上面的例子来自this article ,建议您使用 Equitable包以简化流程。 This article也值得一读。
关于dart - 如何比较同一类型的两个对象的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54006947/