Repository navigation
[mockito] Fake a subclass for sealed return types when stubbing - #5264
AbdeldjalilChougui wants to merge 1 commit into
Conversation
A sealed class cannot be faked, so a method returning one needed provideDummy before it could be stubbed. For the returnValue passed to noSuchMethod, which is discarded after stubbing, generate a fake of a subclass of the sealed class instead. The value used for missing stubs in nice mocks is unchanged, so that the choice of a subclass never affects the code under test. Part of dart-lang#4462.
|
Thanks. It will take me a while to get to this as I am travelling then will be on vacation for a week; I'll get to it when back. |
davidmorgan
left a comment
There was a problem hiding this comment.
This looks good to me. Using the subclass fake only for returnValue is the right line to draw: Mock.noSuchMethod returns it only while when, verify or untilCalled is in progress, so the subclass chosen never reaches code under test.
I tried a few inputs beyond the PR's tests. For a return type of Result<int> with subclasses Ok<T> extends Result<T>, Str extends Result<String> and Err extends Result<Never>, it skips the generic and wrong-type subclasses and fakes Err. A sealed type used only as a parameter generates the same code as before. The builder tests pass.
One optional idea inline. Not checked: compiling the generated code. The builder tests only match its text.
| /// Direct subtypes of a sealed class are declared in the same library. | ||
| Iterable<ClassElement> _directSubtypesOfSealed(InterfaceElement element) { | ||
| if (element is! ClassElement || !element.isSealed) return const []; | ||
| return element.library.classes.where( |
There was a problem hiding this comment.
Optional: library.classes leaves out enums, so a sealed class whose only implementor is an enum, sealed class Bar {} with enum E implements Bar { a }, still falls back to dummyValue<Bar>() at run time. The first enum value would work as the stubbing value, as _dummyValueImplementing already does for enum types. Fine to leave for a follow-up.
Part of #4462. This implements the idea @yanok described there (and the
TODOin_dummyValueImplementing).A sealed class can't be faked, so for a method like
Pet getPet()the generated mock falls back todummyValue<Pet>()at run time, and stubbing throwsMissingDummyValueErrorunless the test callsprovideDummy.With this change, when the value is the
returnValuepassed tonoSuchMethod(which is discarded once the stub or verification is recorded), the generator looks for a subclass of the sealed class that can be faked and emits a fake of that:A subclass is used if it is public, has no type parameters, and is not
base,finalorsealed. Sealed subclasses are searched recursively. If there is none, the output is the same as before.returnValueForMissingStubis not changed, following the comment in the issue: a nice mock should not silently pick one subclass as its default. So this fixes the@GenerateMockscase; nice mocks still needprovideDummyfor a sealed return type, which is why this is "part of" and not "fixes".To make the subclass importable,
_TypeVisitornow also collects the direct subtypes of a sealed class it visits.Testing:
Future<Sealed>, a nested sealed class, subclasses that can't be faked, no usable subclass, and the nice-mock default.@GenerateMocks:when(repo.getPet(1)).thenReturn(cat)passes withoutprovideDummy, and throwsMissingDummyValueError: Petwithout this change.