felangel / mocktail

A mock library for Dart inspired by mockito
https://pub.dev/packages/mocktail
MIT License
617 stars 81 forks source link

How mock a final class #196

Closed lsaudon closed 1 year ago

lsaudon commented 1 year ago

Describe the issue I try to update my package to dart 3. I have this error The class 'ProcessResult' can't be implemented outside of its library because it's a final class.

class _MockProcessResult extends Mock implements ProcessResult {}
renancaraujo commented 1 year ago

AFAIK this is only possible with some goodwill from the package maintainers.

If you have access to the class library (which is not the case for ProcessResult), the solution is to provide the interface of the class as a test-only interface class:

@visibleForTesting
interface class YouCantMockMeInterface {
 /// mockable stuff, go here
}

final class YouCantMockMe implements YouCantMockMeInterface {}

In this way, mocks can be done on methods and elements that use the YouCantMockMeInterface as type.