1
1
import 'dart:async' ;
2
2
3
3
import 'package:disposebag/disposebag.dart' ;
4
-
5
- // import 'package:mockito/mockito.dart';
6
4
import 'package:test/test.dart' ;
7
5
6
+ import 'mocks.dart' ;
7
+
8
8
Stream <int > _getPeriodicStream () =>
9
9
Stream .periodic (const Duration (milliseconds: 100 ), (i) => i).take (10 );
10
10
11
11
const _maxCount = 5 ;
12
12
13
- class MockDisposeBag /*extends Mock*/ implements DisposeBag {
14
- var _disposeCount = 0 ;
15
-
16
- @override
17
- dynamic noSuchMethod (Invocation invocation) {
18
- if (invocation.memberName == #dispose) {
19
- ++ _disposeCount;
20
- return Future .value (true );
21
- }
22
- return super .noSuchMethod (invocation);
23
- }
24
-
25
- void verifyCalledDispose (int expected) => expect (_disposeCount, expected);
26
- }
27
-
28
- class MockSink /*extends Mock*/ implements Sink <int > {
29
- @override
30
- dynamic noSuchMethod (Invocation invocation) => super .noSuchMethod (invocation);
31
-
32
- @override
33
- Future <void > close () async {}
34
- }
35
-
36
- class MockStreamSubscription /*extends Mock*/
37
- implements
38
- StreamSubscription <int > {
39
- @override
40
- dynamic noSuchMethod (Invocation invocation) => super .noSuchMethod (invocation);
41
-
42
- @override
43
- Future <void > cancel () async {}
44
- }
45
-
46
13
void main () {
47
14
group ('DisposeBag' , () {
48
15
group ('DisposeBag.add' , () {
@@ -101,9 +68,9 @@ void main() {
101
68
await bag.add (controller);
102
69
await bag.dispose ();
103
70
104
- /*verify (sink.close()).called(1);*/ //TODO
71
+ expect (sink.call, const MethodCall ( 'close' ));
105
72
expect (controller.isClosed, true );
106
- }, skip : true );
73
+ });
107
74
108
75
test ('DisposeBag.add.sink.isDisposed' , () async {
109
76
final controller = StreamController <int >()..stream.listen (null );
@@ -376,26 +343,24 @@ void main() {
376
343
final disposeBag = DisposeBag ();
377
344
final mockStreamSubscription = MockStreamSubscription ();
378
345
379
- /*when(mockStreamSubscription.cancel()).thenAnswer(
380
- (realInvocation) async => throw Exception(),
381
- );*/ //TODO
346
+ mockStreamSubscription.whenCancel =
347
+ () => Future .error (Exception ());
382
348
383
349
await disposeBag.add (mockStreamSubscription);
384
350
expect (await disposeBag.dispose (), false );
385
351
expect (disposeBag.isDisposed, false );
386
- }, skip : true );
352
+ });
387
353
388
354
test ('DisposeBag.clear.failed' , () async {
389
355
final disposeBag = DisposeBag ();
390
356
final mockStreamSubscription = MockStreamSubscription ();
391
357
392
- /*when(mockStreamSubscription.cancel()).thenAnswer(
393
- (realInvocation) async => throw Exception(),
394
- );*/ //TODO
358
+ mockStreamSubscription.whenCancel =
359
+ () => Future .error (Exception ());
395
360
396
361
await disposeBag.add (mockStreamSubscription);
397
362
expect (await disposeBag.clear (), false );
398
- }, skip : true );
363
+ });
399
364
400
365
test ('issue #2' , () async {
401
366
final bag = DisposeBag ();
@@ -420,21 +385,21 @@ void main() {
420
385
});
421
386
422
387
group ('Extensions' , () {
423
- late DisposeBag bag;
388
+ late MockDisposeBag bag;
424
389
425
390
setUp (() => bag = MockDisposeBag ());
426
391
427
392
test ('StreamSubscription.disposedBy' , () {
428
393
final subscription = Stream .value (1 ).listen (null );
429
394
subscription.disposedBy (bag);
430
- /*verify (bag.add( subscription)).called(1);*/ //TODO
431
- }, skip : true );
395
+ expect (bag.call, MethodCall ( 'add' , subscription));
396
+ });
432
397
433
398
test ('Sink.disposedBy' , () {
434
399
final controller = StreamController <void >();
435
400
controller.disposedBy (bag);
436
- /*verify (bag.add( controller)).called(1);*/ //TODO
437
- }, skip : true );
401
+ expect (bag.call, MethodCall ( 'add' , controller));
402
+ });
438
403
439
404
test ('Iterable<StreamSubscription>.disposedBy' , () {
440
405
final subscription1 = Stream .value (1 ).listen (null );
@@ -443,8 +408,8 @@ void main() {
443
408
final subscriptions = [subscription1, subscription2];
444
409
subscriptions.disposedBy (bag);
445
410
446
- /*verify (bag.addAll( subscriptions)).called(1);*/ //TODO
447
- }, skip : true );
411
+ expect (bag.call, MethodCall ( 'addAll' , subscriptions));
412
+ });
448
413
449
414
test ('Iterable<Sink>.disposedBy' , () {
450
415
final controller1 = StreamController <void >();
@@ -453,7 +418,7 @@ void main() {
453
418
final sinks = [controller1, controller2];
454
419
sinks.disposedBy (bag);
455
420
456
- /*verify (bag.addAll( sinks)).called(1);*/ //TODO
457
- }, skip : true );
421
+ expect (bag.call, MethodCall ( 'addAll' , sinks));
422
+ });
458
423
});
459
424
}
0 commit comments