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))`.
diff --git a/CHANGELOG.md b/CHANGELOG.md
index ce1deb1..4c1f150 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,7 @@
+## 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))`.
+
## 0.10.0
* Added support for spy.
diff --git a/README.md b/README.md
index cbdcae2..36e9316 100644
--- a/README.md
+++ b/README.md
@@ -17,6 +17,7 @@
class Cat {
String sound() => "Meow";
bool eatFood(String food, {bool hungry}) => true;
+ int walk(List<String> places);
void sleep(){}
int lives = 9;
}
@@ -66,20 +67,25 @@
```dart
//you can use arguments itself...
when(cat.eatFood("fish")).thenReturn(true);
+//..or collections
+when(cat.walk(["roof","tree"])).thenReturn(2);
//..or matchers
when(cat.eatFood(argThat(startsWith("dry"))).thenReturn(false);
//..or mix aguments with matchers
when(cat.eatFood(argThat(startsWith("dry")), true).thenReturn(true);
expect(cat.eatFood("fish"), isTrue);
+expect(cat.walk(["roof","tree"]), equals(2));
expect(cat.eatFood("dry food"), isFalse);
expect(cat.eatFood("dry food", hungry: true), isTrue);
//you can also verify using an argument matcher
verify(cat.eatFood("fish"));
+verify(cat.walk(["roof","tree"]));
verify(cat.eatFood(argThat(contains("food"))));
//you can verify setters
cat.lives = 9;
verify(cat.lives=9);
```
+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))`.
Argument matchers allow flexible verification or stubbing
## Verifying exact number of invocations / at least x / never
diff --git a/lib/mockito.dart b/lib/mockito.dart
index 3c7313d..1b36761 100644
--- a/lib/mockito.dart
+++ b/lib/mockito.dart
@@ -171,7 +171,7 @@
// } else if(roleArg is Mock){
// return identical(roleArg, actArg);
} else {
- return roleArg == actArg;
+ return equals(roleArg).matches(actArg, {});
}
}
}
diff --git a/pubspec.yaml b/pubspec.yaml
index 34f0096..4596018 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -1,5 +1,5 @@
name: mockito
-version: 0.10.1
+version: 0.11.0
author: Dmitriy Fibulwinter <fibulwinter@gmail.com>
description: A mock framework inspired by Mockito.
homepage: https://github.com/fibulwinter/dart-mockito
diff --git a/test/mockito_test.dart b/test/mockito_test.dart
index 2b39cbe..03a2ed9 100644
--- a/test/mockito_test.dart
+++ b/test/mockito_test.dart
@@ -4,9 +4,11 @@
class RealClass {
String methodWithoutArgs() => "Real";
String methodWithNormalArgs(int x) => "Real";
+ String methodWithListArgs(List<int> x) => "Real";
String methodWithPositionalArgs(int x, [int y]) => "Real";
String methodWithNamedArgs(int x, {int y}) => "Real";
String methodWithTwoNamedArgs(int x, {int y, int z}) => "Real";
+ String methodWithObjArgs(RealClass x) => "Real";
String get getter => "Real";
void set setter(String arg) {
throw new StateError("I must be mocked");
@@ -90,6 +92,12 @@
expect(mock.methodWithNormalArgs(43), isNull);
expect(mock.methodWithNormalArgs(42), equals("Ultimate Answer"));
});
+ test("should mock method with mock args", () {
+ var m1 = new MockedClass();
+ when(mock.methodWithObjArgs(m1)).thenReturn("Ultimate Answer");
+ expect(mock.methodWithObjArgs(new MockedClass()), isNull);
+ expect(mock.methodWithObjArgs(m1), equals("Ultimate Answer"));
+ });
test("should mock method with positional args", () {
when(mock.methodWithPositionalArgs(42, 17)).thenReturn("Answer and...");
expect(mock.methodWithPositionalArgs(42), isNull);
@@ -102,6 +110,11 @@
expect(mock.methodWithNamedArgs(42, y: 18), isNull);
expect(mock.methodWithNamedArgs(42, y: 17), equals("Why answer?"));
});
+ test("should mock method with List args", () {
+ when(mock.methodWithListArgs([42])).thenReturn("Ultimate answer");
+ expect(mock.methodWithListArgs([43]), isNull);
+ expect(mock.methodWithListArgs([42]), equals("Ultimate answer"));
+ });
test("should mock method with argument matcher", () {
when(mock.methodWithNormalArgs(argThat(greaterThan(100))))
.thenReturn("A lot!");
@@ -113,6 +126,11 @@
expect(mock.methodWithNormalArgs(100), equals("A lot!"));
expect(mock.methodWithNormalArgs(101), equals("A lot!"));
});
+ test("should mock method with any list argument matcher", () {
+ when(mock.methodWithListArgs(any)).thenReturn("A lot!");
+ expect(mock.methodWithListArgs([42]), equals("A lot!"));
+ expect(mock.methodWithListArgs([43]), equals("A lot!"));
+ });
test("should mock method with multiple named args and matchers", (){
when(mock.methodWithTwoNamedArgs(any, y: any)).thenReturn("x y");
when(mock.methodWithTwoNamedArgs(any, z: any)).thenReturn("x z");
@@ -230,6 +248,25 @@
});
verify(mock.methodWithNamedArgs(42, y: 17));
});
+ test("should mock method with mock args", () {
+ var m1 = named(new MockedClass(), name: "m1");
+ mock.methodWithObjArgs(m1);
+ expectFail(
+ "No matching calls. All calls: MockedClass.methodWithObjArgs(m1)",
+ () {
+ verify(mock.methodWithObjArgs(new MockedClass()));
+ });
+ verify(mock.methodWithObjArgs(m1));
+ });
+ test("should mock method with list args", () {
+ mock.methodWithListArgs([42]);
+ expectFail(
+ "No matching calls. All calls: MockedClass.methodWithListArgs([42])",
+ () {
+ verify(mock.methodWithListArgs([43]));
+ });
+ verify(mock.methodWithListArgs([42]));
+ });
test("should mock method with argument matcher", () {
mock.methodWithNormalArgs(100);
expectFail(
@@ -439,6 +476,10 @@
expect(verify(mock.methodWithNormalArgs(captureAny)).captured.single,
equals(42));
});
+ test("should captureOut list arguments", () {
+ mock.methodWithListArgs([42]);
+ expect(verify(mock.methodWithListArgs(captureAny)).captured.single, equals([42]));
+ });
test("should captureOut multiple arguments", () {
mock.methodWithPositionalArgs(1, 2);
expect(verify(