forked from felangel/mocktail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
verify_test.dart
696 lines (632 loc) · 21.2 KB
/
verify_test.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
import 'package:mocktail/mocktail.dart';
import 'package:test/test.dart';
/// Every method on this class has a nullable return type and nullable parameter
/// types. This allows each method to be verified without manual overriding or
/// code-generation.
class _RealClass {
String? methodWithoutArgs() => 'Real';
String? methodWithNormalArgs(int? x) => 'Real';
String? methodWithListArgs(List<int>? x) => 'Real';
String? methodWithOptionalArg([int? x]) => 'Real';
String? methodWithPositionalArgs(int? x, [int? y]) => 'Real';
String? methodWithNamedArgs(int x, {int? y}) => 'Real';
String? methodWithOnlyNamedArgs({int? y = 0, int? z}) => 'Real';
String? methodWithTypeArgs<T>(int? x) => 'Real';
String? methodWithDefaultTypeArg<T extends num>() => 'Real';
String? get getter => 'Real';
String get nsGetter => 'Real';
set setter(String arg) {
throw StateError('I must be mocked');
}
String? methodWithLongArgs(LongToString? a, LongToString? b,
{LongToString? c, LongToString? d}) =>
'Real';
}
class LongToString {
const LongToString(this.aList, this.aMap, this.aString);
final List aList;
final Map aMap;
final String aString;
@override
String toString() => 'LongToString<\n'
' aList: $aList\n'
' aMap: $aMap\n'
' aString: $aString\n'
'>';
}
class _MockedClass extends Mock implements _RealClass {}
void expectFail(Pattern expectedMessage, void Function() expectedToFail) {
try {
expectedToFail();
fail('It was expected to fail!');
} on TestFailure catch (e) {
expect(e.message,
expectedMessage is String ? expectedMessage : contains(expectedMessage),
reason: 'Failed but with unexpected message');
}
}
const noMatchingCallsFooter = '(If you called `verify(...).called(0);`, '
'please instead use `verifyNever(...);`.)';
void main() {
late _MockedClass mock;
setUp(() {
mock = _MockedClass();
});
tearDown(resetMocktailState);
group('verify', () {
test('should verify method without args', () {
mock.methodWithoutArgs();
verify(() => mock.methodWithoutArgs());
});
test('should verify method with normal args', () {
mock.methodWithNormalArgs(42);
verify(() => mock.methodWithNormalArgs(42));
});
test('should mock method with positional args', () {
mock.methodWithPositionalArgs(42, 17);
expectFail(
'No matching calls. All calls: '
'_MockedClass.methodWithPositionalArgs(42, 17)\n'
'$noMatchingCallsFooter', () {
verify(() => mock.methodWithPositionalArgs(42));
});
expectFail(
'No matching calls. All calls: '
'_MockedClass.methodWithPositionalArgs(42, 17)\n'
'$noMatchingCallsFooter', () {
verify(() => mock.methodWithPositionalArgs(42, 18));
});
verify(() => mock.methodWithPositionalArgs(42, 17));
});
test('should mock method with named args', () {
mock.methodWithNamedArgs(42, y: 17);
expectFail(
'No matching calls. All calls: '
'_MockedClass.methodWithNamedArgs(42, {y: 17})\n'
'$noMatchingCallsFooter', () {
verify(() => mock.methodWithNamedArgs(42));
});
expectFail(
'No matching calls. All calls: '
'_MockedClass.methodWithNamedArgs(42, {y: 17})\n'
'$noMatchingCallsFooter', () {
verify(() => mock.methodWithNamedArgs(42, y: 18));
});
verify(() => mock.methodWithNamedArgs(42, y: 17));
});
test('should verify method with type args', () {
mock.methodWithTypeArgs<List<double>>(42);
expectFail(
'No matching calls. All calls: '
'_MockedClass.methodWithTypeArgs<List<double>>(42)\n'
'$noMatchingCallsFooter', () {
verify(() => mock.methodWithTypeArgs<List<String>>(42));
});
verify(() => mock.methodWithTypeArgs<List<double>>(42));
});
test('should verify method with default type args', () {
mock.methodWithDefaultTypeArg();
expectFail(
'No matching calls. All calls: '
'_MockedClass.methodWithDefaultTypeArg<num>()\n'
'$noMatchingCallsFooter', () {
verify(() => mock.methodWithDefaultTypeArg<int>());
});
verify(() => mock.methodWithDefaultTypeArg());
});
test('should mock method with list args', () {
mock.methodWithListArgs([42]);
expectFail(
'No matching calls. All calls: '
'_MockedClass.methodWithListArgs([42])\n'
'$noMatchingCallsFooter', () {
verify(() => mock.methodWithListArgs([43]));
});
verify(() => mock.methodWithListArgs([42]));
});
test('should mock method with argument matcher', () {
mock.methodWithNormalArgs(100);
expectFail(
'No matching calls. All calls: '
'_MockedClass.methodWithNormalArgs(100)\n'
'$noMatchingCallsFooter', () {
verify(
() => mock.methodWithNormalArgs(any(that: greaterThan(100))),
);
});
verify(
() => mock.methodWithNormalArgs(
any(that: greaterThanOrEqualTo(100)),
),
);
});
test('should mock method with mix of argument matchers and real things',
() {
mock.methodWithPositionalArgs(100, 17);
expectFail(
'No matching calls. All calls: '
'_MockedClass.methodWithPositionalArgs(100, 17)\n'
'$noMatchingCallsFooter', () {
verify(
() => mock.methodWithPositionalArgs(
any(that: greaterThanOrEqualTo(100)),
18,
),
);
});
expectFail(
'No matching calls. All calls: '
'_MockedClass.methodWithPositionalArgs(100, 17)\n'
'$noMatchingCallsFooter', () {
verify(() =>
mock.methodWithPositionalArgs(any(that: greaterThan(100)), 17));
});
verify(() => mock.methodWithPositionalArgs(
any(that: greaterThanOrEqualTo(100)), 17));
});
test('should fail on non-mock object', () {
expectFail('Used on a non-mocktail object', () {
verify(() => Object().toString());
});
});
test('should mock getter', () {
mock.getter;
verify(() => mock.getter);
});
test('should mock setter', () {
mock.setter = 'A';
final expectedMessage = RegExp.escape('No matching calls. '
'All calls: _MockedClass.setter==A\n$noMatchingCallsFooter');
// RegExp needed because of https://github.com/dart-lang/sdk/issues/33565
var expectedPattern = RegExp(expectedMessage.replaceFirst('==', '=?='));
expectFail(expectedPattern, () => verify(() => mock.setter = 'B'));
verify(() => mock.setter = 'A');
});
test(
'should throw meaningful errors '
'when verification is interrupted (1)', () {
// ignore: only_throw_errors
final badHelper = () => throw 'boo';
try {
verify(() => mock.methodWithNamedArgs(42, y: badHelper()));
} catch (_) {}
// At this point, verification was interrupted, so
// `_verificationInProgress` is still `true`. Calling mock methods below
// adds items to `_verifyCalls`.
mock.methodWithNamedArgs(42, y: 17);
try {
verify(() => mock.methodWithNamedArgs(42, y: 17));
fail('verify call was expected to throw!');
} catch (exception) {
expect(
exception,
isA<StateError>().having(
(exception) => exception.message,
'message',
contains(
'Verification appears to be in progress. '
'One verify call has been stored',
),
),
);
}
});
test(
'should throw meaningful errors '
'when verification is interrupted (2)', () {
// ignore: only_throw_errors
final badHelper = () => throw 'boo';
try {
verify(() => mock.methodWithNamedArgs(42, y: badHelper()));
} catch (_) {}
// At this point, verification was interrupted, so
// `_verificationInProgress` is still `true`. Calling mock methods below
// adds items to `_verifyCalls`.
mock
..methodWithNamedArgs(42, y: 17)
..methodWithNamedArgs(42, y: 17);
try {
verify(() => mock.methodWithNamedArgs(42, y: 17));
fail('verify call was expected to throw!');
} catch (exception) {
expect(
exception,
isA<StateError>().having(
(exception) => exception.message,
'message',
contains(
'Verification appears to be in progress. '
'2 verify calls have been stored.',
),
),
);
}
});
});
group('verify should fail when no matching call is found', () {
test('and there are no unmatched calls', () {
expectFail(
'No matching calls (actually, no calls at all).\n'
'$noMatchingCallsFooter', () {
verify(() => mock.methodWithNormalArgs(43));
});
});
test('and there is one unmatched call', () {
mock.methodWithNormalArgs(42);
expectFail(
'''No matching calls. All calls: _MockedClass.methodWithNormalArgs(42)\n'''
'$noMatchingCallsFooter', () {
verify(() => mock.methodWithNormalArgs(43));
});
});
test('and there is one unmatched call without args', () {
mock.methodWithOptionalArg();
expectFail(
'''No matching calls. All calls: _MockedClass.methodWithOptionalArg(null)\n'''
'$noMatchingCallsFooter', () {
verify(() => mock.methodWithOptionalArg(43));
});
});
test('and there are multiple unmatched calls', () {
mock
..methodWithNormalArgs(41)
..methodWithNormalArgs(42);
expectFail(
'No matching calls. All calls: '
'_MockedClass.methodWithNormalArgs(41), '
'_MockedClass.methodWithNormalArgs(42)\n'
'$noMatchingCallsFooter', () {
verify(() => mock.methodWithNormalArgs(43));
});
});
test('and unmatched calls have only named args', () {
mock.methodWithOnlyNamedArgs(y: 1);
expectFail(
'No matching calls. All calls: '
'_MockedClass.methodWithOnlyNamedArgs({y: 1, z: null})\n'
'$noMatchingCallsFooter', () {
verify(() => mock.methodWithOnlyNamedArgs());
});
});
});
group('verify qualifies', () {
group('unqualified as at least one', () {
test('zero fails', () {
expectFail(
'No matching calls (actually, no calls at all).\n'
'$noMatchingCallsFooter', () {
verify(() => mock.methodWithoutArgs());
});
});
test('one passes', () {
mock.methodWithoutArgs();
verify(() => mock.methodWithoutArgs());
});
test('more than one passes', () {
mock
..methodWithoutArgs()
..methodWithoutArgs();
verify(() => mock.methodWithoutArgs());
});
});
group('expecting one call', () {
test('zero actual calls fails', () {
expectFail(
'No matching calls (actually, no calls at all).\n'
'$noMatchingCallsFooter', () {
verify(() => mock.methodWithoutArgs()).called(1);
});
});
test('one actual call passes', () {
mock.methodWithoutArgs();
verify(() => mock.methodWithoutArgs()).called(1);
});
test('more than one actual call fails', () {
mock
..methodWithoutArgs()
..methodWithoutArgs();
expectFail('Expected: <1>\n Actual: <2>\nUnexpected number of calls\n',
() {
verify(() => mock.methodWithoutArgs()).called(1);
});
});
});
group('expecting more than two calls', () {
test('zero actual calls fails', () {
expectFail(
'No matching calls (actually, no calls at all).\n'
'$noMatchingCallsFooter', () {
verify(() => mock.methodWithoutArgs()).called(greaterThan(2));
});
});
test('one actual call fails', () {
mock.methodWithoutArgs();
expectFail(
'Expected: a value greater than <2>\n'
' Actual: <1>\n'
' Which: is not a value greater than <2>\n'
'Unexpected number of calls\n', () {
verify(() => mock.methodWithoutArgs()).called(greaterThan(2));
});
});
test('three actual calls passes', () {
mock
..methodWithoutArgs()
..methodWithoutArgs()
..methodWithoutArgs();
verify(() => mock.methodWithoutArgs()).called(greaterThan(2));
});
});
group('expecting zero calls', () {
test('zero actual calls passes', () {
expectFail(
'No matching calls (actually, no calls at all).\n'
'$noMatchingCallsFooter', () {
verify(() => mock.methodWithoutArgs()).called(0);
});
});
test('one actual call fails', () {
mock.methodWithoutArgs();
expectFail(
'Expected: <0>\n'
' Actual: <1>\n'
'Unexpected number of calls\n', () {
verify(() => mock.methodWithoutArgs()).called(0);
});
});
});
group('verifyNever', () {
test('zero passes', () {
verifyNever(() => mock.methodWithoutArgs());
});
test('one fails', () {
// Add one verified method that should not appear in message.
mock.methodWithNormalArgs(1);
verify(() => mock.methodWithNormalArgs(1)).called(1);
mock.methodWithoutArgs();
expectFail('Unexpected calls: _MockedClass.methodWithoutArgs()', () {
verifyNever(() => mock.methodWithoutArgs());
});
});
});
group('does not count already verified again', () {
test('fail case', () {
mock.methodWithoutArgs();
verify(() => mock.methodWithoutArgs());
expectFail(
'No matching calls. '
'All calls: [VERIFIED] _MockedClass.methodWithoutArgs()\n'
'$noMatchingCallsFooter', () {
verify(() => mock.methodWithoutArgs());
});
});
test('pass case', () {
mock.methodWithoutArgs();
verify(() => mock.methodWithoutArgs());
mock.methodWithoutArgs();
verify(() => mock.methodWithoutArgs());
});
});
});
group('verifyZeroInteractions', () {
test('never touched pass', () {
verifyZeroInteractions(mock);
});
test('any touch fails', () {
mock.methodWithoutArgs();
expectFail(
'No interaction expected, but following found: '
'_MockedClass.methodWithoutArgs()', () {
verifyZeroInteractions(mock);
});
});
test('verified call fails', () {
mock.methodWithoutArgs();
verify(() => mock.methodWithoutArgs());
expectFail(
'No interaction expected, but following found: '
'[VERIFIED] _MockedClass.methodWithoutArgs()', () {
verifyZeroInteractions(mock);
});
});
test('throws if given a real object', () {
expect(() => verifyZeroInteractions(_RealClass()), throwsArgumentError);
});
});
group('verifyNoMoreInteractions', () {
test('never touched pass', () {
verifyNoMoreInteractions(mock);
});
test('any unverified touch fails', () {
mock.methodWithoutArgs();
expectFail(
'No more calls expected, but following found: '
'_MockedClass.methodWithoutArgs()', () {
verifyNoMoreInteractions(mock);
});
});
test('verified touch passes', () {
mock.methodWithoutArgs();
verify(() => mock.methodWithoutArgs());
verifyNoMoreInteractions(mock);
});
test('throws if given a real object', () {
expect(() => verifyNoMoreInteractions(_RealClass()), throwsArgumentError);
});
});
group('verifyInOrder', () {
test('right order passes', () {
mock
..methodWithoutArgs()
..getter;
verifyInOrder([() => mock.methodWithoutArgs(), () => mock.getter]);
});
test('right order passes (ns)', () {
when(() => mock.nsGetter).thenReturn('mock');
mock
..methodWithoutArgs()
..nsGetter;
verifyInOrder([() => mock.methodWithoutArgs(), () => mock.nsGetter]);
});
test('wrong order fails', () {
mock
..methodWithoutArgs()
..getter;
expectFail(
'Matching call #1 not found. All calls: '
'_MockedClass.methodWithoutArgs(), _MockedClass.getter', () {
verifyInOrder([() => mock.getter, () => mock.methodWithoutArgs()]);
});
});
test('verify been used as an argument fails', () {
mock
..methodWithoutArgs()
..getter;
expectFail(
'There is already a verification in progress, '
'check if it was not called with a verify argument(s)', () {
verifyInOrder([
() => verify(() => mock.getter),
() => verify(() => mock.methodWithoutArgs()),
]);
});
});
test('incomplete fails', () {
mock.methodWithoutArgs();
expectFail(
'Matching call #1 not found. All calls: '
'_MockedClass.methodWithoutArgs()', () {
verifyInOrder([() => mock.methodWithoutArgs(), () => mock.getter]);
});
});
test('methods can be called again and again', () {
mock
..methodWithoutArgs()
..getter
..methodWithoutArgs();
verifyInOrder([
() => mock.methodWithoutArgs(),
() => mock.getter,
() => mock.methodWithoutArgs(),
]);
});
test('can return captures from capturing argument matchers', () {
mock
..methodWithNormalArgs(1)
..methodWithoutArgs()
..methodWithNormalArgs(2);
final captured = verifyInOrder([
() => mock.methodWithNormalArgs(captureAny()),
() => mock.methodWithoutArgs(),
() => mock.methodWithNormalArgs(captureAny()),
]).captured;
expect(captured, hasLength(3));
expect(captured[0], equals([1]));
expect(captured[1], equals(<int>[]));
expect(captured[2], equals([2]));
});
test('methods can be called again and again - fail case', () {
mock
..methodWithoutArgs()
..methodWithoutArgs()
..getter;
expectFail(
'Matching call #2 not found. All calls: '
'_MockedClass.methodWithoutArgs(), _MockedClass.methodWithoutArgs(), '
'_MockedClass.getter', () {
verifyInOrder(
[
() => mock.methodWithoutArgs(),
() => mock.getter,
() => mock.methodWithoutArgs(),
],
);
});
});
test(
'should throw meaningful errors '
'when verification is interrupted', () {
// ignore: only_throw_errors
final badHelper = () => throw 'boo';
try {
verify(() => mock.methodWithNamedArgs(42, y: badHelper()));
} catch (_) {}
// At this point, verification was interrupted, so
// `_verificationInProgress` is still `true`. Calling mock methods below
// adds items to `_verifyCalls`.
mock.methodWithNamedArgs(42, y: 17);
try {
verifyInOrder([() => mock.methodWithNamedArgs(42, y: 17)]);
fail('verify call was expected to throw!');
} catch (exception) {
expect(
exception,
isA<StateError>().having(
(exception) => exception.message,
'message',
contains(
'''VerifyCall<mock: _MockedClass, memberName: Symbol("methodWithNamedArgs")>''',
),
),
);
}
});
});
group('multiline toStrings on objects', () {
test(
'"No matching calls" message visibly separates unmatched calls, '
'if an arg\'s string representations is multiline', () {
mock
..methodWithLongArgs(
const LongToString(
<int>[1, 2],
<int, String>{1: 'a', 2: 'b'},
'c',
),
const LongToString(
<int>[4, 5],
<int, String>{3: 'd', 4: 'e'},
'f',
),
)
..methodWithLongArgs(
null,
null,
c: const LongToString(
<int>[5, 6],
<int, String>{5: 'g', 6: 'h'},
'i',
),
d: const LongToString(
<int>[7, 8], <int, String>{7: 'j', 8: 'k'}, 'l'),
);
expectFail(
'No matching calls. All calls: '
'_MockedClass.methodWithLongArgs(\n'
' LongToString<\n'
' aList: [1, 2]\n'
' aMap: {1: a, 2: b}\n'
' aString: c\n'
' >,\n'
' LongToString<\n'
' aList: [4, 5]\n'
' aMap: {3: d, 4: e}\n'
' aString: f\n'
' >, {c: null, d: null}),\n'
'_MockedClass.methodWithLongArgs(null, null, {\n'
' c: LongToString<\n'
' aList: [5, 6]\n'
' aMap: {5: g, 6: h}\n'
' aString: i\n'
' >,\n'
' d: LongToString<\n'
' aList: [7, 8]\n'
' aMap: {7: j, 8: k}\n'
' aString: l\n'
' >})\n'
'$noMatchingCallsFooter', () {
verify(() => mock.methodWithNormalArgs(43));
});
});
});
}