| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 /** | |
| 6 * A simple mocking/spy library. | |
| 7 * | |
| 8 * To create a mock objects for some class T, create a new class using: | |
| 9 * | |
| 10 * class MockT extends Mock implements T {}; | |
| 11 * | |
| 12 * Then specify the [Behavior] of the Mock for different methods using | |
| 13 * [when] (to select the method and parameters) and then the [Action]s | |
| 14 * for the [Behavior] by calling [thenReturn], [alwaysReturn], [thenThrow], | |
| 15 * [alwaysThrow], [thenCall] or [alwaysCall]. | |
| 16 * | |
| 17 * [thenReturn], [thenThrow] and [thenCall] are one-shot so you would | |
| 18 * typically call these more than once to specify a sequence of actions; | |
| 19 * this can be done with chained calls, e.g.: | |
| 20 * | |
| 21 * m.when(callsTo('foo')). | |
| 22 * thenReturn(0).thenReturn(1).thenReturn(2); | |
| 23 * | |
| 24 * [thenCall] and [alwaysCall] allow you to proxy mocked methods, chaining | |
| 25 * to some other implementation. This provides a way to implement 'spies'. | |
| 26 * | |
| 27 * For getters and setters, use "get foo" and "set foo"-style arguments | |
| 28 * to [callsTo]. | |
| 29 * | |
| 30 * You can disable logging for a particular [Behavior] easily: | |
| 31 * | |
| 32 * m.when(callsTo('bar')).logging = false; | |
| 33 * | |
| 34 * You can then use the mock object. Once you are done, to verify the | |
| 35 * behavior, use [getLogs] to extract a relevant subset of method call | |
| 36 * logs and apply [Matchers] to these through calling [verify]. | |
| 37 * | |
| 38 * A Mock can be given a name when constructed. In this case instead of | |
| 39 * keeping its own log, it uses a shared log. This can be useful to get an | |
| 40 * audit trail of interleaved behavior. It is the responsibility of the user | |
| 41 * to ensure that mock names, if used, are unique. | |
| 42 * | |
| 43 * Limitations: | |
| 44 * | |
| 45 * * only positional parameters are supported (up to 10); | |
| 46 * * to mock getters you will need to include parentheses in the call | |
| 47 * (e.g. m.length() will work but not m.length). | |
| 48 * | |
| 49 * Here is a simple example: | |
| 50 * | |
| 51 * class MockList extends Mock implements List {}; | |
| 52 * | |
| 53 * List m = new MockList(); | |
| 54 * m.when(callsTo('add', anything)).alwaysReturn(0); | |
| 55 * | |
| 56 * m.add('foo'); | |
| 57 * m.add('bar'); | |
| 58 * | |
| 59 * getLogs(m, callsTo('add', anything)).verify(happenedExactly(2)); | |
| 60 * getLogs(m, callsTo('add', 'foo')).verify(happenedOnce); | |
| 61 * getLogs(m, callsTo('add', 'isNull)).verify(neverHappened); | |
| 62 * | |
| 63 * Note that we don't need to provide argument matchers for all arguments, | |
| 64 * but we do need to provide arguments for all matchers. So this is allowed: | |
| 65 * | |
| 66 * m.when(callsTo('add')).alwaysReturn(0); | |
| 67 * m.add(1, 2); | |
| 68 * | |
| 69 * But this is not allowed and will throw an exception: | |
| 70 * | |
| 71 * m.when(callsTo('add', anything, anything)).alwaysReturn(0); | |
| 72 * m.add(1); | |
| 73 * | |
| 74 * Here is a way to implement a 'spy', which is where we log the call | |
| 75 * but then hand it off to some other function, which is the same | |
| 76 * method in a real instance of the class being mocked: | |
| 77 * | |
| 78 * class Foo { | |
| 79 * bar(a, b, c) => a + b + c; | |
| 80 * } | |
| 81 * | |
| 82 * class MockFoo extends Mock implements Foo { | |
| 83 * Foo real; | |
| 84 * MockFoo() { | |
| 85 * real = new Foo(); | |
| 86 * this.when(callsTo('bar')).alwaysCall(real.bar); | |
| 87 * } | |
| 88 * } | |
| 89 * | |
| 90 * However, there is an even easier way, by calling [Mock.spy], e.g.: | |
| 91 * | |
| 92 * var foo = new Foo(); | |
| 93 * var spy = new Mock.spy(foo); | |
| 94 * print(spy.bar(1, 2, 3)); | |
| 95 * | |
| 96 * Spys created with Mock.spy do not have user-defined behavior; | |
| 97 * they are simply proxies, and thus will throw an exception if | |
| 98 * you call [when]. They capture all calls in the log, so you can | |
| 99 * do assertions on their history, such as: | |
| 100 * | |
| 101 * spy.getLogs(callsTo('bar')).verify(happenedOnce); | |
| 102 * | |
| 103 * [pub]: http://pub.dartlang.org | |
| 104 */ | |
| 105 | |
| 106 library mock; | |
| 107 | |
| 108 export 'src/action.dart'; | |
| 109 export 'src/behavior.dart'; | |
| 110 export 'src/call_matcher.dart'; | |
| 111 export 'src/log_entry.dart'; | |
| 112 export 'src/log_entry_list.dart'; | |
| 113 export 'src/mock.dart'; | |
| 114 export 'src/responder.dart'; | |
| 115 export 'src/result_matcher.dart'; | |
| 116 export 'src/result_set_matcher.dart'; | |
| 117 export 'src/times_matcher.dart'; | |
| OLD | NEW |