Skip to content

Commit aa9e03b

Browse files
committed
0.11.0 Equality matcher used by default to simplify matching collections as arguments.
Should be non-breaking change in most cases, otherwise consider using `argThat(identical(arg))`.
1 parent 9d1d5b1 commit aa9e03b

File tree

5 files changed

+53
-2
lines changed

5 files changed

+53
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.11.0
2+
3+
* Equality matcher used by default to simplify matching collections as arguments. Should be non-breaking change in most cases, otherwise consider using `argThat(identical(arg))`.
4+
15
## 0.10.0
26

37
* Added support for spy.

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import 'package:mockito/mockito.dart';
1717
class Cat {
1818
String sound() => "Meow";
1919
bool eatFood(String food, {bool hungry}) => true;
20+
int walk(List<String> places);
2021
void sleep(){}
2122
int lives = 9;
2223
}
@@ -66,20 +67,25 @@ Last stubbing is more important - when you stubbed the same method with the same
6667
```dart
6768
//you can use arguments itself...
6869
when(cat.eatFood("fish")).thenReturn(true);
70+
//..or collections
71+
when(cat.walk(["roof","tree"])).thenReturn(2);
6972
//..or matchers
7073
when(cat.eatFood(argThat(startsWith("dry"))).thenReturn(false);
7174
//..or mix aguments with matchers
7275
when(cat.eatFood(argThat(startsWith("dry")), true).thenReturn(true);
7376
expect(cat.eatFood("fish"), isTrue);
77+
expect(cat.walk(["roof","tree"]), equals(2));
7478
expect(cat.eatFood("dry food"), isFalse);
7579
expect(cat.eatFood("dry food", hungry: true), isTrue);
7680
//you can also verify using an argument matcher
7781
verify(cat.eatFood("fish"));
82+
verify(cat.walk(["roof","tree"]));
7883
verify(cat.eatFood(argThat(contains("food"))));
7984
//you can verify setters
8085
cat.lives = 9;
8186
verify(cat.lives=9);
8287
```
88+
By default equals matcher is used to argument matching (since 0.11.0). It simplifies matching for collections as arguments. If you need more strict matching consider use `argThat(identical(arg))`.
8389
Argument matchers allow flexible verification or stubbing
8490

8591
## Verifying exact number of invocations / at least x / never

lib/mockito.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ class InvocationMatcher {
171171
// } else if(roleArg is Mock){
172172
// return identical(roleArg, actArg);
173173
} else {
174-
return roleArg == actArg;
174+
return equals(roleArg).matches(actArg, {});
175175
}
176176
}
177177
}

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: mockito
2-
version: 0.10.1
2+
version: 0.11.0
33
author: Dmitriy Fibulwinter <[email protected]>
44
description: A mock framework inspired by Mockito.
55
homepage: https://github.com/fibulwinter/dart-mockito

test/mockito_test.dart

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ import 'package:mockito/mockito.dart';
44
class RealClass {
55
String methodWithoutArgs() => "Real";
66
String methodWithNormalArgs(int x) => "Real";
7+
String methodWithListArgs(List<int> x) => "Real";
78
String methodWithPositionalArgs(int x, [int y]) => "Real";
89
String methodWithNamedArgs(int x, {int y}) => "Real";
910
String methodWithTwoNamedArgs(int x, {int y, int z}) => "Real";
11+
String methodWithObjArgs(RealClass x) => "Real";
1012
String get getter => "Real";
1113
void set setter(String arg) {
1214
throw new StateError("I must be mocked");
@@ -90,6 +92,12 @@ void main() {
9092
expect(mock.methodWithNormalArgs(43), isNull);
9193
expect(mock.methodWithNormalArgs(42), equals("Ultimate Answer"));
9294
});
95+
test("should mock method with mock args", () {
96+
var m1 = new MockedClass();
97+
when(mock.methodWithObjArgs(m1)).thenReturn("Ultimate Answer");
98+
expect(mock.methodWithObjArgs(new MockedClass()), isNull);
99+
expect(mock.methodWithObjArgs(m1), equals("Ultimate Answer"));
100+
});
93101
test("should mock method with positional args", () {
94102
when(mock.methodWithPositionalArgs(42, 17)).thenReturn("Answer and...");
95103
expect(mock.methodWithPositionalArgs(42), isNull);
@@ -102,6 +110,11 @@ void main() {
102110
expect(mock.methodWithNamedArgs(42, y: 18), isNull);
103111
expect(mock.methodWithNamedArgs(42, y: 17), equals("Why answer?"));
104112
});
113+
test("should mock method with List args", () {
114+
when(mock.methodWithListArgs([42])).thenReturn("Ultimate answer");
115+
expect(mock.methodWithListArgs([43]), isNull);
116+
expect(mock.methodWithListArgs([42]), equals("Ultimate answer"));
117+
});
105118
test("should mock method with argument matcher", () {
106119
when(mock.methodWithNormalArgs(argThat(greaterThan(100))))
107120
.thenReturn("A lot!");
@@ -113,6 +126,11 @@ void main() {
113126
expect(mock.methodWithNormalArgs(100), equals("A lot!"));
114127
expect(mock.methodWithNormalArgs(101), equals("A lot!"));
115128
});
129+
test("should mock method with any list argument matcher", () {
130+
when(mock.methodWithListArgs(any)).thenReturn("A lot!");
131+
expect(mock.methodWithListArgs([42]), equals("A lot!"));
132+
expect(mock.methodWithListArgs([43]), equals("A lot!"));
133+
});
116134
test("should mock method with multiple named args and matchers", (){
117135
when(mock.methodWithTwoNamedArgs(any, y: any)).thenReturn("x y");
118136
when(mock.methodWithTwoNamedArgs(any, z: any)).thenReturn("x z");
@@ -230,6 +248,25 @@ void main() {
230248
});
231249
verify(mock.methodWithNamedArgs(42, y: 17));
232250
});
251+
test("should mock method with mock args", () {
252+
var m1 = named(new MockedClass(), name: "m1");
253+
mock.methodWithObjArgs(m1);
254+
expectFail(
255+
"No matching calls. All calls: MockedClass.methodWithObjArgs(m1)",
256+
() {
257+
verify(mock.methodWithObjArgs(new MockedClass()));
258+
});
259+
verify(mock.methodWithObjArgs(m1));
260+
});
261+
test("should mock method with list args", () {
262+
mock.methodWithListArgs([42]);
263+
expectFail(
264+
"No matching calls. All calls: MockedClass.methodWithListArgs([42])",
265+
() {
266+
verify(mock.methodWithListArgs([43]));
267+
});
268+
verify(mock.methodWithListArgs([42]));
269+
});
233270
test("should mock method with argument matcher", () {
234271
mock.methodWithNormalArgs(100);
235272
expectFail(
@@ -439,6 +476,10 @@ void main() {
439476
expect(verify(mock.methodWithNormalArgs(captureAny)).captured.single,
440477
equals(42));
441478
});
479+
test("should captureOut list arguments", () {
480+
mock.methodWithListArgs([42]);
481+
expect(verify(mock.methodWithListArgs(captureAny)).captured.single, equals([42]));
482+
});
442483
test("should captureOut multiple arguments", () {
443484
mock.methodWithPositionalArgs(1, 2);
444485
expect(verify(

0 commit comments

Comments
 (0)