dart-lang / mockito

Mockito-inspired mock library for Dart
https://pub.dev/packages/mockito
Apache License 2.0
623 stars 160 forks source link

Autogenerate const constructors for Mocks #680

Closed danielgomezrico closed 11 months ago

danielgomezrico commented 11 months ago

Hi

Is there any way to make the generated Mock classes to have a const constructor by default? That may help to make all of the tests to run faster

Current

class MockHttpClientService extends _i1.Mock implements _i44.HttpClientService {
  MockHttpClientService() {
    _i1.throwOnMissingStub(this);
  }

Expected

WIth a const constructor (just adding the const keyword on the constructor)

class MockHttpClientService extends _i1.Mock implements _i44.HttpClientService {
  const MockHttpClientService() {
    _i1.throwOnMissingStub(this);
  }
yanok commented 11 months ago

Nah, that won't work, since mock objects have internal state. Like _realCalls here https://github.com/dart-lang/mockito/blob/b4217750cbab5cdd2a773106abb476a7ea4aefb2/lib/src/mock.dart#L133, it is final, but it can't be const, since that will block us from adding anything to it.

yanok commented 11 months ago

(Not to mention your "Expected" code won't compile, since const constructor can't have bodies. But that's a solvable problem, the main issue is mocks are inherently not constant, so we shouldn't declare them as such).

danielgomezrico commented 11 months ago

Oh! got it, thanks for answering :)