Better verification error (#121)
Better verification error
diff --git a/lib/src/mock.dart b/lib/src/mock.dart
index 0e39774..99d1d64 100644
--- a/lib/src/mock.dart
+++ b/lib/src/mock.dart
@@ -565,6 +565,9 @@
inv.verified = true;
});
}
+
+ String toString() =>
+ 'VerifyCall<mock: $mock, memberName: ${verifyInvocation.memberName}>';
}
class ArgMatcher {
@@ -681,7 +684,16 @@
Verification _makeVerify(bool never) {
if (_verifyCalls.isNotEmpty) {
- throw new StateError(_verifyCalls.join());
+ var message = 'Verification appears to be in progress.';
+ if (_verifyCalls.length == 1) {
+ message =
+ '$message One verify call has been stored: ${_verifyCalls.single}';
+ } else {
+ message =
+ '$message ${_verifyCalls.length} verify calls have been stored. '
+ '[${_verifyCalls.first}, ..., ${_verifyCalls.last}]';
+ }
+ throw new StateError(message);
}
_verificationInProgress = true;
return <T>(T mock) {
diff --git a/test/mockito_test.dart b/test/mockito_test.dart
index 49d9d8d..f377720 100644
--- a/test/mockito_test.dart
+++ b/test/mockito_test.dart
@@ -705,6 +705,29 @@
});
verify(mock.setter = "A");
});
+
+ test("should throw meaningful errors when verification is interrupted", () {
+ var badHelper = () => throw 'boo';
+ try {
+ verify(mock.methodWithNamedArgs(42, y: badHelper()));
+ fail("verify call was expected to throw!");
+ } 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);
+ mock.methodWithNamedArgs(42, y: 17);
+ try {
+ verify(mock.methodWithNamedArgs(42, y: 17));
+ fail("verify call was expected to throw!");
+ } catch (e) {
+ expect(e, new isInstanceOf<StateError>());
+ expect(
+ e.message,
+ contains(
+ "Verification appears to be in progress. 2 verify calls have been stored."));
+ }
+ });
});
group("verify() qualifies", () {