Sub topic 14
Testing models with value equality
This chapter assumes you know Dart classes, named parameters, null safety, and basic flutter test usage. It also assumes that Person is a freezed class and that its generated files are current.
A freezed model uses value equality. Two instances are equal when their fields contain equal values, even when the instances are different objects. This makes tests describe the result you expect instead of checking every field separately.
Why value equality improves tests
Consider two separately created objects:
const first = Person(name: 'Ada', age: 36);
const second = Person(name: 'Ada', age: 36);Identity equality would treat them as different because they occupy different locations in memory. Generated value equality treats them as equal because their fields match.
This matters when a repository, service, or state-management class returns a new object after an operation. The test can compare the complete expected model directly.
| Test goal | Useful assertion |
|---|---|
| Models contain the same values | `expect(actual, equals(expected))` |
| A change creates a different value | `expect(updated, isNot(equals(original)))` |
| Equal models work in hashed collections | Compare their `hashCode` values |
| Debug output explains a failure | Use generated `toString()` output |
Writing tests for `copyWith`
Place model tests in the test directory. The following example assumes that Person is exported by lib/person.dart.
import 'package:flutter_test/flutter_test.dart';
import 'package:my_app/person.dart';
void main() {
group('Person', () {
test('uses value equality for matching fields', () {
const first = Person(name: 'Ada', age: 36);
const second = Person(name: 'Ada', age: 36);
expect(first, equals(second));
expect(first.hashCode, equals(second.hashCode));
});
test('copyWith changes only the requested field', () {
const original = Person(name: 'Ada', age: 36);
final updated = original.copyWith(age: 37);
expect(updated, equals(const Person(name: 'Ada', age: 37)));
expect(updated.name, equals(original.name));
expect(original.age, equals(36));
});
test('copyWith preserves the original object', () {
const original = Person(name: 'Ada', age: 36);
original.copyWith(age: 37);
expect(original, equals(const Person(name: 'Ada', age: 36)));
});
});
}Run the test with:
flutter test test/person_test.dartThe first test verifies the central value-object contract. The second checks both the complete result and the field that should remain unchanged. The third protects immutability: calling copyWith must return a new value rather than modifying original.
Testing nested values and collections
Freezed can compare nested freezed objects by value. This allows an expected object to describe an entire result:
test('updates a nested address', () {
const user = User(
name: 'Ada',
address: Address(city: 'London'),
);
final moved = user.copyWith.address(city: 'Oslo');
expect(
moved,
equals(const User(
name: 'Ada',
address: Address(city: 'Oslo'),
)),
);
expect(user.address.city, equals('London'));
});Be careful with collection fields. Freezed makes the model field unassignable, but a mutable list can still be changed through another reference unless the model or constructor protects it. Prefer immutable collection types or create defensive copies at the boundary.
A useful test flow
Common testing mistakes
- Comparing models with
identical, which checks object identity instead of field values. - Forgetting to regenerate files after changing a model.
- Expecting the original instance to change after
copyWith. - Testing only one field and missing an unexpected change in another field.
- Mutating a list after passing it into a model and assuming the model is deeply immutable.
Exercise
What should a test assert after calling original.copyWith(age: 37)?
Show answer
It should assert that the returned value equals the expected model, that the requested field changed, and that the original model still has its old value.