Skip to content

Commit eb4d1da

Browse files
jamesderlinsrawlins
authored andcommitted
Update with review feedback from srawlins
1 parent db19e8c commit eb4d1da

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

FAQ.md

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,12 @@ Also, you can also check out the example configuration in the Mockito repository
207207

208208
## How do I mock a `sealed` class?
209209

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+
210216
Suppose that you have code such as:
211217

212218
```dart
@@ -223,20 +229,21 @@ class Foo {
223229
}
224230
```
225231

226-
and now you want to mock `Foo`. The generated implementation for `MockFoo`
227-
needs to return *something* if `someMethod` is called. It can't return `null`
228-
since its return type is non-nullable. It can't construct a `Bar` on its own
229-
without understanding the semantics of `Bar`'s constructors. That is, which
230-
`Bar` constructor should be called and with what arguments? To avoid this,
231-
Mockito instead generates its own, fake implementation of `Bar` that it does
232-
know how to construct, something like:
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:
233240

234241
```dart
235242
class _FakeBar extends Fake implements Bar {}
236243
```
237244

238-
And then the generated implementation of `MockFoo` can have its `someMethod`
239-
override return a `_FakeBar` instance.
245+
And then `MockFoo` can have its `someMethod` override return a `_FakeBar`
246+
instance.
240247

241248
However, if `Bar` is `sealed` (or is marked with `base` or `final`), then it
242249
cannot be `implemented` in generated code. Therefore Mockito can't generate a

lib/src/dummies.dart

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,9 @@ provide dummy values for some types, even if they plan to explicitly stub all
9595
the called methods. Call either `provideDummy` or `provideDummyBuilder` to
9696
provide Mockito with a dummy value.
9797
98-
For more details, see:
99-
<https://github.com/dart-lang/mockito/blob/master/FAQ.md#how-do-i-mock-a-sealed-class>
98+
For more details, see the questions regarding `sealed` classes in the FAQ:
99+
100+
<https://github.com/dart-lang/mockito/blob/master/FAQ.md>
100101
''';
101102
}
102103

@@ -168,8 +169,10 @@ void provideDummyBuilder<T>(DummyBuilder<T> dummyBuilder) =>
168169
/// Provide a dummy value for `T` to be used both while adding expectations
169170
/// and as a default value for unstubbed methods, if using a nice mock.
170171
///
171-
/// For details and for example usage, see:
172-
/// https://github.com/dart-lang/mockito/blob/master/FAQ.md#how-do-i-mock-a-sealed-class
172+
/// For details and for example usage, see the questions regarding `sealed`
173+
/// classes in the [FAQ].
174+
///
175+
/// [FAQ]: https://github.com/dart-lang/mockito/blob/master/FAQ.md
173176
void provideDummy<T>(T dummy) =>
174177
provideDummyBuilder((parent, invocation) => dummy);
175178

0 commit comments

Comments
 (0)