From 9baeeb987d13d85e81023cbaa726c8af38b19f64 Mon Sep 17 00:00:00 2001 From: Sam Rawlins Date: Wed, 18 Jul 2018 13:58:09 -0700 Subject: [PATCH] Throw an error when stubbing a non-mock method (#159) --- lib/src/mock.dart | 4 ++++ test/mockito_test.dart | 9 +++++++++ 2 files changed, 13 insertions(+) diff --git a/lib/src/mock.dart b/lib/src/mock.dart index 8a398e6e..76010722 100644 --- a/lib/src/mock.dart +++ b/lib/src/mock.dart @@ -348,6 +348,10 @@ class PostExpectation { } void _completeWhen(Answering answer) { + if (_whenCall == null) { + throw new StateError( + 'Mock method was not called within `when()`. Was a real method called?'); + } _whenCall._setExpected(answer); _whenCall = null; _whenInProgress = false; diff --git a/test/mockito_test.dart b/test/mockito_test.dart index 3223cb0d..fc2b62d7 100644 --- a/test/mockito_test.dart +++ b/test/mockito_test.dart @@ -45,6 +45,8 @@ abstract class _AbstractFoo implements _Foo { String bar() => baz(); String baz(); + + String quux() => "Real"; } class _MockFoo extends _AbstractFoo with Mock {} @@ -288,6 +290,13 @@ void main() { .thenReturn("99"); }, throwsArgumentError); }); + + test("should throw if attempting to stub a real method", () { + var foo = new _MockFoo(); + expect(() { + when(foo.quux()).thenReturn("Stub"); + }, throwsStateError); + }); }); group("throwOnMissingStub", () {