Skip to content

Commit

Permalink
Deprecate spy(), make the default no-mirrors.
Browse files Browse the repository at this point in the history
  • Loading branch information
matanlurey committed Oct 7, 2016
1 parent 9820ba3 commit dc8ce18
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 51 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## 2.0.0-dev

* Deprecated usage of `spy` and any `dart:mirrors` based API. Users may
import as `package:mockito/deprecated.dart` until the 2.0.0 final
release.
* Deprecated `mockito_no_mirrors.dart`; replace with `mockito.dart`.

## 1.0.1

* Add a new `thenThrow` method to the API.
Expand Down
5 changes: 5 additions & 0 deletions lib/deprecated.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@Deprecated('Using spy() is deprecated as we drop dart:mirrors usage')
library mockito.deprecated;

export 'mockito.dart';
export 'src/spy.dart' show spy;
31 changes: 29 additions & 2 deletions lib/mockito.dart
Original file line number Diff line number Diff line change
@@ -1,2 +1,29 @@
export 'mockito_no_mirrors.dart';
export 'src/spy.dart' show spy;
export 'src/mock.dart'
show
Mock,
named,

// -- setting behaviour
when,
any,
argThat,
captureAny,
captureThat,
typed,
Answering,
Expectation,
PostExpectation,

// -- verification
verify,
verifyInOrder,
verifyNever,
verifyNoMoreInteractions,
verifyZeroInteractions,
VerificationResult,
Verification,

// -- misc
clearInteractions,
reset,
logInvocations;
31 changes: 3 additions & 28 deletions lib/mockito_no_mirrors.dart
Original file line number Diff line number Diff line change
@@ -1,29 +1,4 @@
export 'src/mock.dart'
show
Mock,
named,
@Deprecated('Import "mockito.dart" instead.')
library mockito.mockito_no_mirrors;

// -- setting behaviour
when,
any,
argThat,
captureAny,
captureThat,
typed,
Answering,
Expectation,
PostExpectation,

// -- verification
verify,
verifyInOrder,
verifyNever,
verifyNoMoreInteractions,
verifyZeroInteractions,
VerificationResult,
Verification,

// -- misc
clearInteractions,
reset,
logInvocations;
export 'mockito.dart';
14 changes: 14 additions & 0 deletions lib/src/spy.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,20 @@ import 'dart:mirrors';

import 'mock.dart' show CannedResponse, setDefaultResponse;

@Deprecated(
'Mockito is dropping support for spy(). Instead it is recommended to come '
'up with a code-generation technique or just hand-coding a Stub object that '
'forwards to a delegate. For example:\n\n'
'class SpyAnimal implements Animal {\n'
' final Animal _delegate;\n'
' FakeAnimal(this._delegate);\n'
'\n'
' @override\n'
' void eat() {\n'
' _delegate.eat();\n'
' }\n'
'}'
)
dynamic spy(dynamic mock, dynamic spiedObject) {
var mirror = reflect(spiedObject);
setDefaultResponse(
Expand Down
23 changes: 2 additions & 21 deletions test/mockito_test.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import 'package:mockito/mockito.dart';
import 'package:mockito/src/mock.dart' show resetMockitoState;
import 'package:test/test.dart';

import 'package:mockito/src/mock.dart';
import 'package:mockito/src/spy.dart';

class RealClass {
String methodWithoutArgs() => "Real";
String methodWithNormalArgs(int x) => "Real";
Expand Down Expand Up @@ -69,24 +68,6 @@ void main() {
resetMockitoState();
});

group("spy", () {
setUp(() {
mock = spy(new MockedClass(), new RealClass());
});

test("should delegate to real object by default", () {
expect(mock.methodWithoutArgs(), 'Real');
});
test("should record interactions delegated to real object", () {
mock.methodWithoutArgs();
verify(mock.methodWithoutArgs());
});
test("should behave as mock when expectation are set", () {
when(mock.methodWithoutArgs()).thenReturn('Spied');
expect(mock.methodWithoutArgs(), 'Spied');
});
});

group("mixin support", () {
test("should work", () {
var foo = new MockFoo();
Expand Down
37 changes: 37 additions & 0 deletions test/spy_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import 'package:mockito/src/mock.dart' show resetMockitoState;
import 'package:mockito/deprecated.dart';
import 'package:test/test.dart';

import 'mockito_test.dart' show MockedClass, RealClass;

void main() {
RealClass mock;

setUp(() {
mock = new MockedClass();
});

tearDown(() {
// In some of the tests that expect an Error to be thrown, Mockito's
// global state can become invalid. Reset it.
resetMockitoState();
});

group("spy", () {
setUp(() {
mock = spy(new MockedClass(), new RealClass());
});

test("should delegate to real object by default", () {
expect(mock.methodWithoutArgs(), 'Real');
});
test("should record interactions delegated to real object", () {
mock.methodWithoutArgs();
verify(mock.methodWithoutArgs());
});
test("should behave as mock when expectation are set", () {
when(mock.methodWithoutArgs()).thenReturn('Spied');
expect(mock.methodWithoutArgs(), 'Spied');
});
});
}

0 comments on commit dc8ce18

Please sign in to comment.