Skip to content

Commit

Permalink
Update with review feedback from srawlins
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesderlin authored and srawlins committed Jul 8, 2024
1 parent db19e8c commit eb4d1da
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
25 changes: 16 additions & 9 deletions FAQ.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,12 @@ Also, you can also check out the example configuration in the Mockito repository

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

`sealed` clases cannot be `extend`ed nor `implement`ed (outside of their Dart
library) and therefore cannot be mocked.


## How do I mock a class that requires instances of a `sealed` class?

Suppose that you have code such as:

```dart
Expand All @@ -223,20 +229,21 @@ class Foo {
}
```

and now you want to mock `Foo`. The generated implementation for `MockFoo`
needs to return *something* if `someMethod` is called. It can't return `null`
since its return type is non-nullable. It can't construct a `Bar` on its own
without understanding the semantics of `Bar`'s constructors. That is, which
`Bar` constructor should be called and with what arguments? To avoid this,
Mockito instead generates its own, fake implementation of `Bar` that it does
know how to construct, something like:
and now you want to mock `Foo`. A mock implementation of `Foo`, generated by
`@GenerateNiceMocks([MockSpec<Foo>()])`, needs to return *something* if
`someMethod` is called. It can't return `null` since its return type is
non-nullable. It can't construct a `Bar` on its own without understanding the
semantics of `Bar`'s constructors. That is, which `Bar` constructor should be
called and with what arguments? To avoid this, Mockito instead generates its
own, fake implementation of `Bar` that it does know how to construct, something
`like:

```dart
class _FakeBar extends Fake implements Bar {}
```

And then the generated implementation of `MockFoo` can have its `someMethod`
override return a `_FakeBar` instance.
And then `MockFoo` can have its `someMethod` override return a `_FakeBar`
instance.

However, if `Bar` is `sealed` (or is marked with `base` or `final`), then it
cannot be `implemented` in generated code. Therefore Mockito can't generate a
Expand Down
11 changes: 7 additions & 4 deletions lib/src/dummies.dart
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,9 @@ provide dummy values for some types, even if they plan to explicitly stub all
the called methods. Call either `provideDummy` or `provideDummyBuilder` to
provide Mockito with a dummy value.
For more details, see:
<https://github.com/dart-lang/mockito/blob/master/FAQ.md#how-do-i-mock-a-sealed-class>
For more details, see the questions regarding `sealed` classes in the FAQ:
<https://github.com/dart-lang/mockito/blob/master/FAQ.md>
''';
}

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

Expand Down

0 comments on commit eb4d1da

Please sign in to comment.