Add new throwOnMissingStub method (#48)
* Add new throwOnMissingStub method
* Bump to 2.0.1 for this fix
diff --git a/CHANGELOG.md b/CHANGELOG.md
index a064372..f5a6633 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,7 @@
+## 2.0.1
+
+* Add a new `throwOnMissingStub` method to the API.
+
## 2.0.0
* Removed `mockito_no_mirrors.dart`
diff --git a/README.md b/README.md
index c8b1c51..7a33508 100644
--- a/README.md
+++ b/README.md
@@ -179,8 +179,10 @@
## Debugging
```dart
-//printing all collected invocations of any mock methods of a list of mock objects
+//print all collected invocations of any mock methods of a list of mock objects
logInvocations([catOne, catTwo]);
+//throw every time that a mock method is called without a stub being matched
+throwOnMissingStub(cat);
```
## Strong mode compliance
diff --git a/lib/src/mock.dart b/lib/src/mock.dart
index c5bfd81..5af92aa 100644
--- a/lib/src/mock.dart
+++ b/lib/src/mock.dart
@@ -32,6 +32,10 @@
mock._defaultResponse = defaultResponse;
}
+throwOnMissingStub(Mock mock) {
+ mock._defaultResponse = Mock._throwResponse;
+}
+
/// Extend or mixin this class to mark the implementation as a [Mock].
///
/// A mocked class implements all fields and methods with a default
@@ -64,9 +68,16 @@
/// used within the context of Dart for Web (dart2js/ddc) and Dart for Mobile
/// (flutter).
class Mock {
- static CannedResponse _nullResponse() {
- return new CannedResponse(null, (_) => null);
- }
+ static CannedResponse _nullResponse() =>
+ new CannedResponse(null, (_) => null);
+
+ static CannedResponse _throwResponse() => new CannedResponse(
+ null,
+ (Invocation inv) =>
+ throw new UnimplementedError('''No stub for invocation:
+ member name: ${inv.memberName}
+ positional arguments (${inv.positionalArguments.length}): ${inv.positionalArguments}
+ named arguments (${inv.namedArguments.length}): ${inv.namedArguments}'''));
final List<RealCall> _realCalls = <RealCall>[];
final List<CannedResponse> _responses = <CannedResponse>[];
diff --git a/pubspec.yaml b/pubspec.yaml
index b5ab855..43ff510 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -1,5 +1,5 @@
name: mockito
-version: 2.0.0
+version: 2.0.1
authors:
- Dmitriy Fibulwinter <fibulwinter@gmail.com>
- Dart Team <misc@dartlang.org>
diff --git a/test/mockito_test.dart b/test/mockito_test.dart
index 2f70958..c69f950 100644
--- a/test/mockito_test.dart
+++ b/test/mockito_test.dart
@@ -13,7 +13,8 @@
// limitations under the License.
import 'package:mockito/mockito.dart';
-import 'package:mockito/src/mock.dart' show resetMockitoState;
+import 'package:mockito/src/mock.dart'
+ show resetMockitoState, throwOnMissingStub;
import 'package:test/test.dart';
class RealClass {
@@ -616,4 +617,19 @@
equals([1, 2]));
});
});
+
+ group("throwOnMissingStub", () {
+ test("should throw when a mock was called without a matching stub", () {
+ throwOnMissingStub(mock as Mock);
+ when(mock.methodWithNormalArgs(42)).thenReturn("Ultimate Answer");
+ expect(() => (mock as MockedClass).methodWithoutArgs(),
+ throwsUnimplementedError);
+ });
+
+ test("should not throw when a mock was called with a matching stub", () {
+ throwOnMissingStub(mock as Mock);
+ when(mock.methodWithoutArgs()).thenReturn("A");
+ expect(() => mock.methodWithoutArgs(), returnsNormally);
+ });
+ });
}