Skip to content

Commit

Permalink
Merge pull request #41 from srawlins/clear-docs
Browse files Browse the repository at this point in the history
Document clearInteractions, reset, and logInvocations
  • Loading branch information
TedSander authored Oct 4, 2016
2 parents 44d8aca + a11c4b2 commit 9820ba3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,14 @@ Verification in order is flexible - you don't have to verify all interactions on
```dart
verifyZeroInteractions(cat);
```

## Finding redundant invocations
```dart
cat.sound();
verify(cat.sound());
verifyNoMoreInteractions(cat);
```

## Capturing arguments for further assertions
```dart
//simple capture
Expand All @@ -141,6 +143,22 @@ cat.eatFood("Milk");
cat.eatFood("Fish");
expect(verify(cat.eatFood(captureThat(startsWith("F")).captured, ["Fish"]);
```

## Resetting mocks
```dart
//clearing collected interactions
cat.eatFood("Fish");
clearInteractions(cat);
cat.eatFood("Fish");
verify(cat.eatFood("Fish")).called(1);
//resetting stubs and collected interactions
when(cat.eatFood("Fish")).thenReturn(true);
cat.eatFood("Fish");
reset(cat);
when(cat.eatFood(any)).thenReturn(false);
expect(cat.eatFood("Fish"), false);
```

## Spy
```dart
//spy creation
Expand All @@ -153,6 +171,12 @@ expect(cat.sound(), "Purr");
expect(cat.lives, 9);
```

## Debugging
```dart
//printing all collected invocations of any mock methods of a list of mock objects
logInvocations([catOne, catTwo]);
```

## Strong mode compliance

Unfortunately, the use of the arg matchers in mock method calls (like `cat.eatFood(any)`)
Expand Down
3 changes: 3 additions & 0 deletions lib/src/mock.dart
Original file line number Diff line number Diff line change
Expand Up @@ -242,11 +242,13 @@ named(var mock, {String name, int hashCode}) => mock
.._givenName = name
.._givenHashCode = hashCode;

/// Clear stubs of, and collected interactions with [mock].
void reset(var mock) {
mock._realCalls.clear();
mock._responses.clear();
}

/// Clear the collected interactions with [mock].
void clearInteractions(var mock) {
mock._realCalls.clear();
}
Expand Down Expand Up @@ -614,6 +616,7 @@ Expectation get when {
};
}

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

0 comments on commit 9820ba3

Please sign in to comment.