dart-lang / mockito

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

Cannot generate mocks for class with method with parameter with default value with a single quote #756

Closed inteist closed 1 week ago

inteist commented 1 week ago

Hi, sorry if this is the wrong place but I am not sure what or where the issue is.

I am getting the following error while running build generator

[SEVERE] mockito:mockBuilder on test/helpers/test_helpers.dart:

Invalid argument(s): Cannot include a single quote in a raw string
package:code_builder/src/specs/expression/literal.dart 51:5  literalString
package:mockito/src/builder.dart 1826:14                     _MockClassInfo._expressionFromDartObject
package:mockito/src/builder.dart 1732:32                     _MockClassInfo._matchingParameter.<fn>
package:code_builder/src/specs/method.g.dart 649:33          _$ParameterBuilder.update
package:code_builder/src/specs/method.g.dart 398:32          new _$Parameter
package:mockito/src/builder.dart 1720:12                     _MockClassInfo._matchingParameter
package:mockito/src/builder.dart 1333:37                     _MockClassInfo._buildOverridingMethod.<fn>
package:mockito/src/builder.dart 2070:24                     _MockClassInfo._withTypeParameters
package:mockito/src/builder.dart 1299:5                      _MockClassInfo._buildOverridingMethod
package:mockito/src/builder.dart 1271:36                     _MockClassInfo.methodOverrides.<fn>
package:code_builder/src/specs/method.g.dart 323:33          _$MethodBuilder.update
package:code_builder/src/specs/method.g.dart 38:29           new _$Method
package:mockito/src/builder.dart 1271:15                     _MockClassInfo.methodOverrides
dart:core                                                    List.addAll
package:built_collection/src/list/list_builder.dart 98:14    ListBuilder.addAll
package:mockito/src/builder.dart 1185:16                     _MockClassInfo._buildMockClass.<fn>.<fn>
package:mockito/src/builder.dart 2070:24                     _MockClassInfo._withTypeParameters
package:mockito/src/builder.dart 1135:7                      _MockClassInfo._buildMockClass.<fn>
package:code_builder/src/specs/class.g.dart 345:33           _$ClassBuilder.update
package:code_builder/src/specs/class.g.dart 40:28            new _$Class
package:mockito/src/builder.dart 1113:12                     _MockClassInfo._buildMockClass
package:mockito/src/builder.dart 1059:9                      new _MockLibraryInfo
package:mockito/src/builder.dart 94:29                       MockBuilder.build

It has suddenly appeared and I am at loss... I have been trying to figure this out for several hours with no valid leads as to what the issue is or what the source is.

Thank you for your help!

srawlins commented 1 week ago

Thanks for the report. Can you send us a minimal reproduction case? We'd need to see the @GenerateNiceMocks call, and the definition of the classes being mocked. Thanks!

inteist commented 1 week ago

Thanks for such a prompt response. I am using a stacked package (framework) and this is the stacked generated code:

@GenerateMocks([], customMocks: [
  MockSpec<NavigationService>(onMissingStub: OnMissingStub.returnDefault),

  MockSpec<BottomSheetService>(onMissingStub: OnMissingStub.returnDefault),

  MockSpec<DialogService>(onMissingStub: OnMissingStub.returnDefault),

  MockSpec<PackageInfoService>(onMissingStub: OnMissingStub.returnDefault),

  MockSpec<RemoteConfigService>(onMissingStub: OnMissingStub.returnDefault),

  MockSpec<DeviceInfoService>(onMissingStub: OnMissingStub.returnDefault),

...

I noticed it uses @GenerateMocks instead of NiceMocks

when i replaced it with NiceMocks - the error is gone but my mock file is not generated (might be a separate issue, I have ravaged my project trying to solve this)

Does that provide any insight and can you possibly direct me towards solution to my issue?

Thanks!

inteist commented 1 week ago

Sorry, my previous reply is crap. It did not generate the issue, because the call was wrong.

@GenerateNiceMocks([
  MockSpec<NavigationService>(onMissingStub: OnMissingStub.returnDefault),

  MockSpec<BottomSheetService>(onMissingStub: OnMissingStub.returnDefault),
 ...

produces the same error.

inteist commented 1 week ago

after 6 hours of digging around and finally resorting to Git backtracking, I found the issue:

I had this in one of the dialogs:

      {String confirmationText = "Are you sure you want to continue?'"}) async {

note the ' right before last " what a trip because of this... 🤦

and I am not sure who's at "fault" - me, stacked or mockito...

srawlins commented 1 week ago

Thanks for the repro! Mockito should be able to handle this; the bug is either in mockito or code_builder, but most likely mockito.

srawlins commented 1 week ago

Ah yeah, pretty simple bug in mockito. Fix is in progress.