From eb4d1daa20c105c94ac29689c1975f0850fa18f2 Mon Sep 17 00:00:00 2001 From: James Lin Date: Wed, 3 Jul 2024 09:41:59 -0700 Subject: [PATCH] Update with review feedback from srawlins --- FAQ.md | 25 ++++++++++++++++--------- lib/src/dummies.dart | 11 +++++++---- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/FAQ.md b/FAQ.md index fad80540..5e3676d5 100644 --- a/FAQ.md +++ b/FAQ.md @@ -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 @@ -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()])`, 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 diff --git a/lib/src/dummies.dart b/lib/src/dummies.dart index 1b29638d..0ac18657 100644 --- a/lib/src/dummies.dart +++ b/lib/src/dummies.dart @@ -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: - +For more details, see the questions regarding `sealed` classes in the FAQ: + + '''; } @@ -168,8 +169,10 @@ void provideDummyBuilder(DummyBuilder 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 dummy) => provideDummyBuilder((parent, invocation) => dummy);