I need to write unit tests for a Flutter project and I would be happy if there is a function that can go through all properties of two different objects of a same type to make sure that all values are same.
Code sample:
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);
});
The equals() method of the Object class compare the equality of two objects. The two objects will be equal if they share the same memory address. Syntax: public boolean equals(Object obj)
Referential equality JavaScript provides 3 ways to compare values: The strict equality operator === The loose equality operator == Object.is() function.
There are two basic formats to Comparing or Contrasting two items. If one were to compare apples and oranges, for example, we would consider the fruits the items, and qualities such as flavor, color, texture, "juicability" and the like as the aspects.
To determine if two objects are not identical Set up a Boolean expression to test the two objects. In your testing expression, use the IsNot operator with the two objects as operands. IsNot returns True if the objects do not point to the same class instance.
==
for equality testingHere is an example of overriding the ==
operator so that you can compare two objects of the same type.
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;
}
The example above comes from this article, which is recommending that you use the Equitable package to simplify the process. This article is also worth reading.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With