Loading...
Loading...
Best practices for using `expect` and `package:matcher`. Focuses on readable assertions, proper matcher selection, and avoiding common pitfalls.
npx skill4agent add kevmoo/dash_skills dart-matcher-best-practicesexpectpackage:matcherhasLengthcontainsisEmptyunorderedEqualscontainsPairhasLength(n)expect(list, hasLength(n))expect(list.length, n)isEmptyisNotEmptyexpect(list, isEmpty)expect(list.isEmpty, true)expect(list, isNotEmpty)expect(list.isNotEmpty, true)contains(item)expect(list.contains(item), true)unorderedEquals(items)expect(list, containsAll(items))containsPair(key, value)expect(map[key], value)expect(map.containsKey(key), true)isA<T>TypeMatcher<T>isA<T>()expect(obj, isA<Type>())TypeMatcher<Type>().having()TypeMatcher<T>constconst isMyType = TypeMatcher<MyType>();.having()consthaving.having()isA<T>()(e) => e.messagep0expect(person, isA<Person>()
.having((p) => p.name, 'name', 'Alice')
.having((p) => p.age, 'age', greaterThan(18)));completion(matcher)await expectLater(...)await expectLater(future, completion(equals(42)))throwsA(matcher)await expectLater(future, throwsA(isA<StateError>()))expect(() => function(), throwsA(isA<ArgumentError>()))expectexpectLaterawait expectLater(...)// GOOD: Waits for future to complete before checking side effects
await expectLater(future, completion(equals(42)));
expect(sideEffectState, equals('done'));
// BAD: Side effect check might run before future completes
expect(future, completion(equals(42)));
expect(sideEffectState, equals('done')); // Race condition!ifforcontainsPairpackage:matcherpackage:checks