Sub topic 6
Matching Arguments with `any` and Literal Values
Mocktail matches the arguments in a call when it stubs behavior or verifies an interaction. You can match an exact value, accept any value, or combine matchers with named arguments. This lets a test focus on the input that matters without making every detail part of the assertion.
Literal values for exact calls
A literal argument matches only the same value. Use literals when the test depends on a particular identifier or option.
import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart';
abstract class WeatherRepository {
Future<String> fetch(
String city, {
bool detailed = false,
});
}
class WeatherService {
WeatherService(this.repository);
final WeatherRepository repository;
Future<String> summary(String city) {
return repository.fetch(city, detailed: false);
}
}
class MockWeatherRepository extends Mock
implements WeatherRepository {}
void main() {
late MockWeatherRepository repository;
late WeatherService service;
setUp(() {
repository = MockWeatherRepository();
service = WeatherService(repository);
});
test('uses the requested city and option', () async {
when(() => repository.fetch('Paris', detailed: false))
.thenAnswer((_) async => 'Sunny');
expect(await service.summary('Paris'), 'Sunny');
verify(() => repository.fetch('Paris', detailed: false))
.called(1);
});
}The stub applies only to fetch('Paris', detailed: false). A call for another city, or a call with detailed: true, does not use this stub.
Accepting any value with `any`
Use any() when the exact value is not important. For example, a service may need to fetch some city, but the test may only care that the repository returned a result.
test('returns the repository result for any city', () async {
when(() => repository.fetch(any(), detailed: false))
.thenAnswer((_) async => 'Unknown');
expect(await service.summary('Rome'), 'Unknown');
expect(await service.summary('Tokyo'), 'Unknown');
verify(() => repository.fetch(any(), detailed: false))
.called(2);
});When you use a matcher for a positional argument, use matchers consistently for the other arguments at that call site. A named argument also needs a named matcher when it is matched generally:
when(() => repository.fetch(
any(),
detailed: any(named: 'detailed'),
)).thenAnswer((_) async => 'Forecast');The named value must exactly match the parameter name in the method declaration. Writing any() without named: 'detailed' does not match that named parameter correctly.
| Form | Meaning | Example |
|---|---|---|
| Literal | Match one exact value | `fetch('Paris')` |
| `any()` | Match any positional value | `fetch(any())` |
| Named `any` | Match any value for one named parameter | `detailed: any(named: 'detailed')` |
| `isA<T>()` | Match any value of a type | `fetch(isA<String>())` |
Matchers in verification
The same matchers work with verify. Use literals when you want to prove the exact request was made. Use any() when the request value is irrelevant.
flowchart TD
A[Test invokes service] --> B[Mock receives method call]
B --> C{Arguments match stub?}
C -->|Literal values match| D[Return configured result]
C -->|any or named matcher matches| D
C -->|No match| E[Mocktail reports missing stub]
D --> F[verify checks the interaction]caption: How mocktail applies argument matchers alt: A flowchart showing a service call reaching a mock, matching either literal or general arguments, returning a result, and then being verified.
Common mistakes
A matcher describes one argument position. It does not inspect the object unless you give it another matcher, such as isA<String>() or predicate.
Avoid mixing a literal and a matcher casually in one invocation:
// Prefer matchers for both positional arguments.
when(() => repository.fetch(any(), detailed: any(named: 'detailed')))
.thenAnswer((_) async => 'Forecast');If a custom non-nullable type causes an error about a missing fallback value, register a Fake for that type before using any(). Primitive types such as String and bool normally need no custom fallback.
Exercise
You stub repository.fetch('Paris', detailed: false), but the production code calls repository.fetch('Paris', detailed: true). Which result does mocktail use?
Show answer
The stub does not match because the named argument differs. Add a second stub or change the production call, depending on the intended behavior.