TypeMatcher<T> class
Null safety
A Matcher subclass that supports validating the Type of the target object.
expect(shouldBeDuration, TypeMatcher<Duration>());
If you want to further validate attributes of the specified Type, use the having function.
void shouldThrowRangeError(int value) {
throw RangeError.range(value, 10, 20);
}
expect(
() => shouldThrowRangeError(5),
throwsA(const TypeMatcher<RangeError>()
.having((e) => e.start, 'start', greaterThanOrEqualTo(10))
.having((e) => e.end, 'end', lessThanOrEqualTo(20))));
Notice that you can chain multiple calls to having to verify multiple aspects of an object.
Note: All of the top-level isType
matchers exposed by this package are
instances of TypeMatcher, so you can use the having function without
creating your own instance.
expect(
() => shouldThrowRangeError(5),
throwsA(isRangeError
.having((e) => e.start, 'start', greaterThanOrEqualTo(10))
.having((e) => e.end, 'end', lessThanOrEqualTo(20))));
- Inheritance
- Implementers
Constructors
- TypeMatcher([@Deprecated('Provide a type argument to TypeMatcher and omit the name. ' 'This argument will be removed in the next release.') String? name])
-
Create a matcher matches instances of type
T
.const
Properties
- hashCode → int
-
The hash code for this object.
read-onlyinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
read-onlyinherited
Methods
-
describe(
Description description) → Description -
Builds a textual description of the matcher.
override
-
describeMismatch(
dynamic item, Description mismatchDescription, Map matchState, bool verbose) → Description -
Builds a textual description of a specific mismatch.
override
-
having(
Object? feature(T), String description, dynamic matcher) → TypeMatcher< T> -
Returns a new TypeMatcher that validates the existing type as well as
a specific
feature
of the object with the providedmatcher
. -
matches(
Object? item, Map matchState) → bool -
Does the matching of the actual vs expected values.
override
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a non-existent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited