1
- // ignore_for_file: sdk_version_async_exported_from_core
2
- // ignore_for_file: unawaited_futures
3
1
import 'package:mockito/annotations.dart' ;
4
2
import 'package:mockito/mockito.dart' ;
5
3
import 'package:test/test.dart' ;
@@ -35,7 +33,10 @@ abstract class Callbacks {
35
33
Cat ,
36
34
Callbacks ,
37
35
], customMocks: [
38
- MockSpec <Cat >(as : #MockCatRelaxed , returnNullOnMissingStub: true ),
36
+ MockSpec <Cat >(
37
+ as : #MockCatRelaxed ,
38
+ onMissingStub: OnMissingStub .returnDefault,
39
+ ),
39
40
])
40
41
void main () {
41
42
late Cat cat;
@@ -57,14 +58,8 @@ void main() {
57
58
});
58
59
59
60
test ('How about some stubbing?' , () {
60
- try {
61
- cat.sound ();
62
- } on MissingStubError {
63
- // Unstubbed methods throw MissingStubError.
64
- }
65
-
66
- // Unstubbed methods return null.
67
- expect (cat.sound (), null );
61
+ // Unstubbed methods throw MissingStubError.
62
+ expect (() => cat.sound (), throwsA (isA <MissingStubError >()));
68
63
69
64
// Stub a method before interacting with it.
70
65
when (cat.sound ()).thenReturn ('Purr' );
@@ -102,7 +97,7 @@ void main() {
102
97
// ... or matchers
103
98
when (cat.eatFood (argThat (startsWith ('dry' )))).thenReturn (false );
104
99
105
- // ... or mix aguments with matchers
100
+ // ... or mix arguments with matchers
106
101
when (cat.eatFood (argThat (startsWith ('dry' )), hungry: true ))
107
102
.thenReturn (true );
108
103
expect (cat.eatFood ('fish' ), isTrue);
@@ -239,7 +234,12 @@ void main() {
239
234
// You can call it without stubbing.
240
235
cat.sleep ();
241
236
242
- // Returns null unless you stub it.
237
+ // Non-null properties and function return values
238
+ // default to reasonable defaults.
239
+ expect (cat.lives, 0 );
240
+
241
+ // Nullable properties and function return values
242
+ // default to null unless you stub them.
243
243
expect (cat.sound (), null );
244
244
expect (cat.eatFood ('Milk' ), null );
245
245
0 commit comments