|
1 | 1 | # Frequently asked questions |
2 | 2 |
|
3 | | -## How do I mock a static method, constructor, or top-level function? |
| 3 | +#### How do I mock a static method, constructor, or top-level function? |
4 | 4 |
|
5 | 5 | Mockito provides its stubbing and verification features by overriding class |
6 | 6 | instance methods. Since there is no mechanism for overriding static methods, |
@@ -58,12 +58,12 @@ for production and a [MemoryFileSystem] for tests), and use its wrapper methods |
58 | 58 | [io package]: https://pub.dev/packages/io |
59 | 59 | [ProcessManager]: https://pub.dev/documentation/io/latest/io/ProcessManager-class.html |
60 | 60 |
|
61 | | -## How do I mock an extension method? |
| 61 | +#### How do I mock an extension method? |
62 | 62 |
|
63 | 63 | If there is no way to override some kind of function, then mockito cannot mock |
64 | 64 | it. See the above answer for further explanation, and alternatives. |
65 | 65 |
|
66 | | -## Why can a method call not be verified multiple times? |
| 66 | +#### Why can a method call not be verified multiple times? |
67 | 67 |
|
68 | 68 | When mockito verifies a method call (via [`verify`] or [`verifyInOrder`]), it |
69 | 69 | marks the call as "verified", which excludes the call from further |
@@ -100,7 +100,7 @@ expect(firstCall, equals(["birds"])); |
100 | 100 | expect(secondCall, equals(["lizards"])); |
101 | 101 | ``` |
102 | 102 |
|
103 | | -## How does mockito work? |
| 103 | +#### How does mockito work? |
104 | 104 |
|
105 | 105 | The basics of the `Mock` class are nothing special: It uses `noSuchMethod` to |
106 | 106 | catch all method invocations, and returns the value that you have configured |
@@ -169,7 +169,7 @@ it's done. It's very straightforward. |
169 | 169 | [`verifyInOrder`]: https://pub.dev/documentation/mockito/latest/mockito/verifyInOrder.html |
170 | 170 |
|
171 | 171 |
|
172 | | -## How can I customize where Mockito outputs its mocks? |
| 172 | +### How can I customize where Mockito outputs its mocks? |
173 | 173 |
|
174 | 174 | Mockito supports configuration of outputs by the configuration provided by the `build` |
175 | 175 | package by creating (if it doesn't exist already) the `build.yaml` at the root folder |
@@ -203,78 +203,3 @@ targets: |
203 | 203 | ``` |
204 | 204 |
|
205 | 205 | Also, you can also check out the example configuration in the Mockito repository. |
206 | | - |
207 | | - |
208 | | -## How do I mock a `sealed` class? |
209 | | - |
210 | | -`sealed` clases cannot be `extend`ed nor `implement`ed (outside of their Dart |
211 | | -library) and therefore cannot be mocked. |
212 | | - |
213 | | - |
214 | | -## How do I mock a class that requires instances of a `sealed` class? |
215 | | - |
216 | | -Suppose that you have code such as: |
217 | | - |
218 | | -```dart |
219 | | -class Bar { |
220 | | - int value; |
221 | | -
|
222 | | - Bar(this.value); |
223 | | -
|
224 | | - Bar.zero() : value = 0; |
225 | | -} |
226 | | -
|
227 | | -class Foo { |
228 | | - Bar someMethod(int value) => Bar(value); |
229 | | -} |
230 | | -``` |
231 | | - |
232 | | -and now you want to mock `Foo`. A mock implementation of `Foo`, generated by |
233 | | -`@GenerateNiceMocks([MockSpec<Foo>()])`, needs to return *something* if |
234 | | -`someMethod` is called. It can't return `null` since its return type is |
235 | | -non-nullable. It can't construct a `Bar` on its own without understanding the |
236 | | -semantics of `Bar`'s constructors. That is, which `Bar` constructor should be |
237 | | -called and with what arguments? To avoid this, Mockito instead generates its |
238 | | -own, fake implementation of `Bar` that it does know how to construct, something |
239 | | -`like: |
240 | | - |
241 | | -```dart |
242 | | -class _FakeBar extends Fake implements Bar {} |
243 | | -``` |
244 | | - |
245 | | -And then `MockFoo` can have its `someMethod` override return a `_FakeBar` |
246 | | -instance. |
247 | | - |
248 | | -However, if `Bar` is `sealed` (or is marked with `base` or `final`), then it |
249 | | -cannot be `implemented` in generated code. Therefore Mockito can't generate a |
250 | | -default value for a `Bar` on its own, and it needs users to specify the default |
251 | | -value to use via `provideDummy` or `provideDummyBuilder`: |
252 | | - |
253 | | -```dart |
254 | | -import 'package:mockito/annotations.dart'; |
255 | | -import 'package:mockito/mockito.dart'; |
256 | | -
|
257 | | -@GenerateNiceMocks([MockSpec<Foo>()]) |
258 | | -import 'foo.mocks.dart'; |
259 | | -
|
260 | | -sealed class Bar { |
261 | | - int value; |
262 | | -
|
263 | | - Bar(this.value); |
264 | | -
|
265 | | - Bar.zero() : value = 0; |
266 | | -} |
267 | | -
|
268 | | -class Foo { |
269 | | - Bar someMethod(int value) => Bar(value); |
270 | | -} |
271 | | -
|
272 | | -void main() { |
273 | | - provideDummy(Bar.zero()); |
274 | | -
|
275 | | - var mockFoo = MockFoo(); |
276 | | -} |
277 | | -``` |
278 | | - |
279 | | -Note that the value used as the "dummy" usually doesn't matter since methods on |
280 | | -the mock typically will be stubbed, overriding the method's return value. |
0 commit comments