OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * A simple mocking/spy library. | 6 * A simple mocking/spy library. |
7 * | 7 * |
8 * ## Installing ## | |
9 * | |
10 * Use [pub][] to install this package. Add the following to your `pubspec.yaml` | |
11 * file. | |
12 * | |
13 * dependencies: | |
14 * unittest: any | |
15 * | |
16 * Then run `pub install`. | |
17 * | |
18 * Import this into your Dart code with: | |
19 * | |
20 * import 'package:unittest/mock.dart'; | |
21 * | |
22 * For more information, see the [unittest package on pub.dartlang.org] | |
23 * (http://pub.dartlang.org/packages/unittest). | |
24 * | |
25 * ## Using ## | |
26 * | |
27 * To create a mock objects for some class T, create a new class using: | 8 * To create a mock objects for some class T, create a new class using: |
28 * | 9 * |
29 * class MockT extends Mock implements T {}; | 10 * class MockT extends Mock implements T {}; |
30 * | 11 * |
31 * Then specify the [Behavior] of the Mock for different methods using | 12 * Then specify the [Behavior] of the Mock for different methods using |
32 * [when] (to select the method and parameters) and then the [Action]s | 13 * [when] (to select the method and parameters) and then the [Action]s |
33 * for the [Behavior] by calling [thenReturn], [alwaysReturn], [thenThrow], | 14 * for the [Behavior] by calling [thenReturn], [alwaysReturn], [thenThrow], |
34 * [alwaysThrow], [thenCall] or [alwaysCall]. | 15 * [alwaysThrow], [thenCall] or [alwaysCall]. |
35 * | 16 * |
36 * [thenReturn], [thenThrow] and [thenCall] are one-shot so you would | 17 * [thenReturn], [thenThrow] and [thenCall] are one-shot so you would |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 * Spys created with Mock.spy do not have user-defined behavior; | 96 * Spys created with Mock.spy do not have user-defined behavior; |
116 * they are simply proxies, and thus will throw an exception if | 97 * they are simply proxies, and thus will throw an exception if |
117 * you call [when]. They capture all calls in the log, so you can | 98 * you call [when]. They capture all calls in the log, so you can |
118 * do assertions on their history, such as: | 99 * do assertions on their history, such as: |
119 * | 100 * |
120 * spy.getLogs(callsTo('bar')).verify(happenedOnce); | 101 * spy.getLogs(callsTo('bar')).verify(happenedOnce); |
121 * | 102 * |
122 * [pub]: http://pub.dartlang.org | 103 * [pub]: http://pub.dartlang.org |
123 */ | 104 */ |
124 | 105 |
125 library unittest.mock; | 106 library mock; |
126 | 107 |
127 import 'dart:mirrors'; | 108 import 'dart:mirrors'; |
128 import 'dart:collection' show LinkedHashMap; | 109 import 'dart:collection' show LinkedHashMap; |
129 | 110 |
130 import 'matcher.dart'; | 111 import 'package:matcher/matcher.dart'; |
131 | 112 |
132 /** | 113 /** |
133 * The error formatter for mocking is a bit different from the default one | 114 * The error formatter for mocking is a bit different from the default one |
134 * for unit testing; instead of the third argument being a 'reason' | 115 * for unit testing; instead of the third argument being a 'reason' |
135 * it is instead a [signature] describing the method signature filter | 116 * it is instead a [signature] describing the method signature filter |
136 * that was used to select the logs that were verified. | 117 * that was used to select the logs that were verified. |
137 */ | 118 */ |
138 String _mockingErrorFormatter(actual, Matcher matcher, String signature, | 119 String _mockingErrorFormatter(actual, Matcher matcher, String signature, |
139 Map matchState, bool verbose) { | 120 Map matchState, bool verbose) { |
140 var description = new StringDescription(); | 121 var description = new StringDescription(); |
(...skipping 1402 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1543 } | 1524 } |
1544 } | 1525 } |
1545 } | 1526 } |
1546 | 1527 |
1547 /** Clear both logs and behavior. */ | 1528 /** Clear both logs and behavior. */ |
1548 void reset() { | 1529 void reset() { |
1549 resetBehavior(); | 1530 resetBehavior(); |
1550 clearLogs(); | 1531 clearLogs(); |
1551 } | 1532 } |
1552 } | 1533 } |
OLD | NEW |