mocktail
Use when creating mocks, stubbing methods, verifying interactions, registering fallback values, or choosing between mocks, fakes, and real objects (…
它会碰到什么
这一栏是扫描器报的事实,不是结论。命中多不等于有毒(安全工具、规则库、示例脚本本来就会包含危险写法),命中少也不等于干净。它和你手上的凭据、文件、网络有什么关系,需要你自己看。
技能内容
Mocktail Skill
This skill defines how to correctly use the mocktail package for mocking in Dart and Flutter tests.
1. Mock vs. Fake vs. Real Object
| Use | When |
|---|---|
| Real object | Prefer over mocks when practical. |
| Fake (extends Fake) | Lightweight custom implementation; override only the methods you need. Prefer over mocks when you don't need interaction verification. |
| Mock (extends Mock) | Only when you need to verify interactions (call counts, arguments) or stub dynamic responses. |
- Never add
@overridemethods or implementations to a class extendingMock. - Only use mocks if your test has
verifyassertions; otherwise prefer real or fake objects.
2. Creating Mocks
class MockMyService extends Mock implements MyService {}
class FakeMyEvent extends Fake implements MyEvent {}
No code generation required — unlike Mockito, Mocktail uses noSuchMethod at runtime.
3. Registering Fallback Values
Register fallback values before using custom types with argument matchers. Do this in setUpAll or at the top of your test:
setUpAll(() {
registerFallbackValue(FakeMyEvent());
});
- Required for non-nullable custom types used with
any(),captureAny(), orcaptureThat(). - Register fallback values for any custom type used with argument matchers.
4. Stubbing
final mock = MockMyService();
// Return a value
when(() => mock.fetchData()).thenReturn('result');
// Throw an error
when(() => mock.fetchData()).thenThrow(Exception('error'));
// Dynamic/async response
when(() => mock.fetchData()).thenAnswer((_) async => 'result');
// Future<void>
when(() => mock.doWork()).thenAnswer((_) async {});
- Always stub async methods (returning
FutureorFuture<void>) withthenAnswer. - Stub every method you expect to be called, even if it's not the focus of your test.
5. Named Parameters
Always include all named parameters in both when and verify calls. Use any(named: 'paramName') for those you don't care about:
when(() => mock.fetch(
id: any(named: 'id'),
headers: any(named: 'headers'),
)).thenReturn(response);
- If a method has default values for named parameters, Mocktail still expects them all to be matched.
6. Verification
verify(() => mock.fetchData()); // called at least once
verifyNever(() => mock.fetchData()); // never called
verify(() => mock.fetchData()).called(2); // called exactly twice
7. Argument Matchers
// Any positional argument
when(() => mock.process(any())).thenReturn(true);
// Capture arguments for later assertions
final captured = verify(() => mock.process(captureAny())).captured;
print(captured.last);
- Use
any()for positional parameters when you don't care about the exact value. - Use
captureThat()for conditional capturing. - When matching string output, be aware of what
.toString()returns for the type.
References
想直接用这个技能?
本站把开放许可(MIT / Apache 等)的技能按仓库打包整理到网盘,点一下转存到你自己的网盘,不用一个个从 GitHub 拉。许可未声明的技能只给原始仓库链接,不打包。