Skip to content

Commit d08d9e1

Browse files
author
Jan-Derk
committed
Added tests
1 parent f61ac89 commit d08d9e1

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

test/method_channel_mock.dart

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import 'package:flutter/services.dart';
2+
3+
class MethodChannelMock {
4+
final MethodChannel methodChannel;
5+
final String method;
6+
final dynamic result;
7+
final Duration delay;
8+
9+
MethodChannelMock({
10+
required String channelName,
11+
required this.method,
12+
this.result,
13+
this.delay = Duration.zero,
14+
}) : methodChannel = MethodChannel(channelName) {
15+
methodChannel.setMockMethodCallHandler(_handler);
16+
}
17+
18+
Future _handler(MethodCall methodCall) async {
19+
if (methodCall.method != method) {
20+
throw MissingPluginException('No implementation found for method '
21+
'$method on channel ${methodChannel.name}');
22+
}
23+
24+
return Future.delayed(delay, () {
25+
if (result is Exception) {
26+
throw result;
27+
}
28+
29+
return Future.value(result);
30+
});
31+
}
32+
}

0 commit comments

Comments
 (0)