I am trying to test my Page with Golden_Toolkit package in Flutter
<code>group('AudioPlayer Test', (){
late MockAudioPlayer mockAudioPlayer;
setUp(() {
mockAudioPlayer = MockAudioPlayer();
});
//TODO cant mock audio native method
testGoldens('admin collection page', (tester) async {
when(mockAudioPlayer.play(any)).thenAnswer((_) async => 1);
final builder = DeviceBuilder()
..addScenario(
widget: const MaterialApp(
debugShowCheckedModeBanner: false, home: AdminCollectionPage()));
await tester.pumpDeviceBuilder(builder);
await screenMatchesGolden(tester, 'admin_collection_page');
});
});
</code>
<code>group('AudioPlayer Test', (){
late MockAudioPlayer mockAudioPlayer;
setUp(() {
mockAudioPlayer = MockAudioPlayer();
});
//TODO cant mock audio native method
testGoldens('admin collection page', (tester) async {
when(mockAudioPlayer.play(any)).thenAnswer((_) async => 1);
final builder = DeviceBuilder()
..addScenario(
widget: const MaterialApp(
debugShowCheckedModeBanner: false, home: AdminCollectionPage()));
await tester.pumpDeviceBuilder(builder);
await screenMatchesGolden(tester, 'admin_collection_page');
});
});
</code>
group('AudioPlayer Test', (){
late MockAudioPlayer mockAudioPlayer;
setUp(() {
mockAudioPlayer = MockAudioPlayer();
});
//TODO cant mock audio native method
testGoldens('admin collection page', (tester) async {
when(mockAudioPlayer.play(any)).thenAnswer((_) async => 1);
final builder = DeviceBuilder()
..addScenario(
widget: const MaterialApp(
debugShowCheckedModeBanner: false, home: AdminCollectionPage()));
await tester.pumpDeviceBuilder(builder);
await screenMatchesGolden(tester, 'admin_collection_page');
});
});
This page uses AudioPlayer and path_provider packages, i can mock path_provider functions with
<code>TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
.setMockMethodCallHandler(
const MethodChannel('plugins.flutter.io/path_provider'),
(MethodCall methodCall) async {
return '.';
});
</code>
<code>TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
.setMockMethodCallHandler(
const MethodChannel('plugins.flutter.io/path_provider'),
(MethodCall methodCall) async {
return '.';
});
</code>
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
.setMockMethodCallHandler(
const MethodChannel('plugins.flutter.io/path_provider'),
(MethodCall methodCall) async {
return '.';
});
But i still cant mock AudioPlayer class with method above.
Logs below:
<code>══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════
The following MissingPluginException was thrown running a test (but after the test had completed):
MissingPluginException(No implementation found for method create on channel xyz.luan/audioplayers)
When the exception was thrown, this was the stack:
#0 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:332:7)
<asynchronous suspension>
#1 AudioplayersPlatform.create (package:audioplayers_platform_interface/src/audioplayers_platform.dart:18:5)
<asynchronous suspension>
#2 AudioPlayer._create (package:audioplayers/src/audioplayer.dart:175:7)
<asynchronous suspension>
</code>
<code>══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════
The following MissingPluginException was thrown running a test (but after the test had completed):
MissingPluginException(No implementation found for method create on channel xyz.luan/audioplayers)
When the exception was thrown, this was the stack:
#0 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:332:7)
<asynchronous suspension>
#1 AudioplayersPlatform.create (package:audioplayers_platform_interface/src/audioplayers_platform.dart:18:5)
<asynchronous suspension>
#2 AudioPlayer._create (package:audioplayers/src/audioplayer.dart:175:7)
<asynchronous suspension>
</code>
══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════
The following MissingPluginException was thrown running a test (but after the test had completed):
MissingPluginException(No implementation found for method create on channel xyz.luan/audioplayers)
When the exception was thrown, this was the stack:
#0 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:332:7)
<asynchronous suspension>
#1 AudioplayersPlatform.create (package:audioplayers_platform_interface/src/audioplayers_platform.dart:18:5)
<asynchronous suspension>
#2 AudioPlayer._create (package:audioplayers/src/audioplayer.dart:175:7)
<asynchronous suspension>
New contributor
Руслан Савельев is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.