easy_mock_system_channel 0.1.0 copy "easy_mock_system_channel: ^0.1.0" to clipboard
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 other easy_mock_* mocks, after the unmocked-channel guard so it can wrap it.
  • mockSystemChannel.platform / .navigation / .mouseCursor / .contextMenu / .restoration — each is a MockMethodChannel, so use .when(...) to override and .verify(...) to assert. Reach any other channel by name with channel('flutter/status_bar').
  • Seeded non-null defaults: Clipboard.hasStrings{'value': false}, getKeyboardState{}.

Notes #

  • flutter/textinput is not mocked by default — the test framework's TestTextInput owns it, and replacing it breaks tester.enterText. Pass install(includeTextInput: true) only if you intend to drive text input yourself.
  • Requires the Flutter test binding (use inside testWidgets, or call TestWidgetsFlutterBinding.ensureInitialized() in a plain test).
  • VM/widget tests only — it mocks the channels, it does not talk to a device.
0
likes
140
points
120
downloads

Documentation

API reference

Publisher

verified publisherahmedomar.com.ly

Weekly Downloads

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 defaults (each method returns null and is recorded) so they never reach the real platform — override and verify per channel. Uses the correct codec per channel; no extra dependencies.

Repository (GitHub)
View/report issues

License

MIT (license)

Dependencies

easy_mock_channel, flutter, flutter_test

More

Packages that depend on easy_mock_system_channel