easy_mock_system_channel 0.1.0
easy_mock_system_channel: ^0.1.0 copied to clipboard
Default test mocks for Flutter's SystemChannels, built on easy_mock_channel. Answers HapticFeedback / SystemChrome / SystemSound / Clipboard and the other framework method channels with sensible defau [...]
easy_mock_system_channel #
Default test mocks for Flutter's SystemChannels,
built on easy_mock_channel. One init() and every framework method channel
(flutter/platform, flutter/navigation, flutter/mousecursor, …) answers with
a sensible default — each method returns null and is recorded — so app code
that calls HapticFeedback.vibrate(), SystemChrome.setPreferredOrientations(),
Clipboard.getData(), SystemSound.play() etc. runs without reaching the real
platform. Each channel is wired with the same codec the framework uses
(e.g. flutter/platform uses JSONMethodCodec).
Usage #
import 'package:easy_mock_system_channel/easy_mock_system_channel.dart';
import 'package:flutter/services.dart';
testWidgets('haptics + clipboard', (tester) async {
mockSystemChannel.init();
// ... drive code that calls HapticFeedback.vibrate() ...
expect(mockSystemChannel.platform.verify(method: 'HapticFeedback.vibrate').length, 1);
// Override a return value:
mockSystemChannel.platform.when(
method: 'Clipboard.getData',
returns: {'text': 'hello'},
);
expect((await Clipboard.getData('text/plain'))?.text, 'hello');
});
mockSystemChannel.init()installs the mocks immediately and removes them on teardown. Wire it into your test harness alongside the othereasy_mock_*mocks, after the unmocked-channel guard so it can wrap it.mockSystemChannel.platform/.navigation/.mouseCursor/.contextMenu/.restoration— each is aMockMethodChannel, so use.when(...)to override and.verify(...)to assert. Reach any other channel by name withchannel('flutter/status_bar').- Seeded non-null defaults:
Clipboard.hasStrings→{'value': false},getKeyboardState→{}.
Notes #
flutter/textinputis not mocked by default — the test framework'sTestTextInputowns it, and replacing it breakstester.enterText. Passinstall(includeTextInput: true)only if you intend to drive text input yourself.- Requires the Flutter test binding (use inside
testWidgets, or callTestWidgetsFlutterBinding.ensureInitialized()in a plaintest). - VM/widget tests only — it mocks the channels, it does not talk to a device.