From a11c4b29d81509eb514a57a42fb4c4b4935e6817 Mon Sep 17 00:00:00 2001 From: Sam Rawlins Date: Mon, 3 Oct 2016 10:34:51 -0700 Subject: [PATCH] Document clearInteractions, reset, and logInvocations --- README.md | 24 ++++++++++++++++++++++++ lib/src/mock.dart | 3 +++ 2 files changed, 27 insertions(+) diff --git a/README.md b/README.md index 974fb7d5..09aec06f 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 @@ -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)`) diff --git a/lib/src/mock.dart b/lib/src/mock.dart index 21f1f5fe..4adf245d 100644 --- a/lib/src/mock.dart +++ b/lib/src/mock.dart @@ -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(); } @@ -614,6 +616,7 @@ Expectation get when { }; } +/// Print all collected invocations of any mock methods of [mocks]. void logInvocations(List mocks) { List allInvocations = mocks.expand((m) => m._realCalls).toList(growable: false);