Skip to content

Commit 9820ba3

Browse files
authored
Merge pull request #41 from srawlins/clear-docs
Document clearInteractions, reset, and logInvocations
2 parents 44d8aca + a11c4b2 commit 9820ba3

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,12 +121,14 @@ Verification in order is flexible - you don't have to verify all interactions on
121121
```dart
122122
verifyZeroInteractions(cat);
123123
```
124+
124125
## Finding redundant invocations
125126
```dart
126127
cat.sound();
127128
verify(cat.sound());
128129
verifyNoMoreInteractions(cat);
129130
```
131+
130132
## Capturing arguments for further assertions
131133
```dart
132134
//simple capture
@@ -141,6 +143,22 @@ cat.eatFood("Milk");
141143
cat.eatFood("Fish");
142144
expect(verify(cat.eatFood(captureThat(startsWith("F")).captured, ["Fish"]);
143145
```
146+
147+
## Resetting mocks
148+
```dart
149+
//clearing collected interactions
150+
cat.eatFood("Fish");
151+
clearInteractions(cat);
152+
cat.eatFood("Fish");
153+
verify(cat.eatFood("Fish")).called(1);
154+
//resetting stubs and collected interactions
155+
when(cat.eatFood("Fish")).thenReturn(true);
156+
cat.eatFood("Fish");
157+
reset(cat);
158+
when(cat.eatFood(any)).thenReturn(false);
159+
expect(cat.eatFood("Fish"), false);
160+
```
161+
144162
## Spy
145163
```dart
146164
//spy creation
@@ -153,6 +171,12 @@ expect(cat.sound(), "Purr");
153171
expect(cat.lives, 9);
154172
```
155173

174+
## Debugging
175+
```dart
176+
//printing all collected invocations of any mock methods of a list of mock objects
177+
logInvocations([catOne, catTwo]);
178+
```
179+
156180
## Strong mode compliance
157181

158182
Unfortunately, the use of the arg matchers in mock method calls (like `cat.eatFood(any)`)

lib/src/mock.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,11 +242,13 @@ named(var mock, {String name, int hashCode}) => mock
242242
.._givenName = name
243243
.._givenHashCode = hashCode;
244244

245+
/// Clear stubs of, and collected interactions with [mock].
245246
void reset(var mock) {
246247
mock._realCalls.clear();
247248
mock._responses.clear();
248249
}
249250

251+
/// Clear the collected interactions with [mock].
250252
void clearInteractions(var mock) {
251253
mock._realCalls.clear();
252254
}
@@ -614,6 +616,7 @@ Expectation get when {
614616
};
615617
}
616618

619+
/// Print all collected invocations of any mock methods of [mocks].
617620
void logInvocations(List<Mock> mocks) {
618621
List<RealCall> allInvocations =
619622
mocks.expand((m) => m._realCalls).toList(growable: false);

0 commit comments

Comments
 (0)