Skip to content

Commit

Permalink
Throw an error when stubbing a non-mock method (#159)
Browse files Browse the repository at this point in the history
  • Loading branch information
srawlins authored and matanlurey committed Jul 18, 2018
1 parent 6fadf54 commit 9baeeb9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/src/mock.dart
Expand Up @@ -348,6 +348,10 @@ class PostExpectation<T> {
}

void _completeWhen(Answering<T> answer) {
if (_whenCall == null) {
throw new StateError(
'Mock method was not called within `when()`. Was a real method called?');
}
_whenCall._setExpected<T>(answer);
_whenCall = null;
_whenInProgress = false;
Expand Down
9 changes: 9 additions & 0 deletions test/mockito_test.dart
Expand Up @@ -45,6 +45,8 @@ abstract class _AbstractFoo implements _Foo {
String bar() => baz();

String baz();

String quux() => "Real";
}

class _MockFoo extends _AbstractFoo with Mock {}
Expand Down Expand Up @@ -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", () {
Expand Down

0 comments on commit 9baeeb9

Please sign in to comment.