Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generated fakes for BuildContext do not include additional parameters for toString #371

Closed
svprdga opened this issue Mar 17, 2021 · 6 comments
Assignees
Labels
P2 A bug or feature request we're likely to work on type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)

Comments

@svprdga
Copy link

svprdga commented Mar 17, 2021

If one tries to generate a mock for BuildContext class, the result mock contains errors.

Steps to reproduce

  1. Use @GenerateMocks annotation to generate a mock of BuildContext

Expected behaviour

An usable MockBuildContext is generated.

Actual behaviour

The following lines of the generated class contains errors:

class _FakeWidget extends _i1.Fake implements _i4.Widget {}

class _FakeInheritedWidget extends _i1.Fake implements _i4.InheritedWidget {}

class _FakeDiagnosticsNode extends _i1.Fake implements _i5.DiagnosticsNode {}

Errors:

 error • 'Object.toString' ('String Function()') isn't a valid concrete implementation of 'Diagnosticable.toString' ('String Function({DiagnosticLevel minLevel})') • test/unit/util/reminders_helper_test.mocks.dart:26:7 • invalid_implementation_override
 error • 'Object.toString' ('String Function()') isn't a valid concrete implementation of 'Diagnosticable.toString' ('String Function({DiagnosticLevel minLevel})') • test/unit/util/reminders_helper_test.mocks.dart:28:7 • invalid_implementation_override
 error • 'Object.toString' ('String Function()') isn't a valid concrete implementation of 'DiagnosticsNode.toString' ('String Function({DiagnosticLevel minLevel, TextTreeConfiguration? parentConfiguration})') • test/unit/util/reminders_helper_test.mocks.dart:30:7 • invalid_implementation_override
@srawlins
Copy link
Member

Similar to #354.

@srawlins
Copy link
Member

I think #342 might offer a solution to this problem, once implemented.

@srawlins srawlins changed the title Generated mocks for BuildContext contains errors Generated mocks for BuildContext do not include additional parameters for toString May 21, 2021
@srawlins srawlins changed the title Generated mocks for BuildContext do not include additional parameters for toString Generated fakes for BuildContext do not include additional parameters for toString Jun 3, 2021
@srawlins srawlins self-assigned this Jun 3, 2021
@srawlins srawlins added P2 A bug or feature request we're likely to work on type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) labels Jun 3, 2021
srawlins added a commit that referenced this issue Jun 4, 2021
…ch the

signature of an overriding method which adds optional parameters.

Fixes #371

PiperOrigin-RevId: 377388828
srawlins added a commit that referenced this issue Jun 4, 2021
…ch the

signature of an overriding method which adds optional parameters.

Fixes #371

PiperOrigin-RevId: 377388828
srawlins added a commit that referenced this issue Jun 4, 2021
…ch the

signature of an overriding method which adds optional parameters.

Fixes #371

PiperOrigin-RevId: 377388828
@kpsroka
Copy link

kpsroka commented Jun 29, 2021

@srawlins It appears that this problem still persists in 5.0.10. Including my minimal repro, and logs:

import 'package:flutter/foundation.dart';
import 'package:mockito/annotations.dart';

part 'test_test.mocks.dart';

class Foo implements Diagnosticable {
  @override
  void debugFillProperties(DiagnosticPropertiesBuilder properties) {}

  @override
  DiagnosticsNode toDiagnosticsNode({String? name, DiagnosticsTreeStyle? style}) {
    return DiagnosticsNode.message('Foo');
  }

  @override
  String toStringShort() => toString();

  @override
  String toString({DiagnosticLevel minLevel = DiagnosticLevel.info}) {
    return super.toString();
  }
}

@GenerateMocks([Foo])
void main(List<String> args) {
}
Logs from `flutter pub run build_runner build --delete-conflicting-outputs --verbose`:
[  +89 ms] executing: sysctl hw.optional.arm64
[  +16 ms] Exit code 1 from: sysctl hw.optional.arm64
[        ] sysctl: unknown oid 'hw.optional.arm64'
[   +7 ms] executing: [/Users/kpsroka/Workspace/SDK/flutter/] git -c log.showSignature=false log -n 1 --pretty=format:%H
[  +15 ms] Exit code 0 from: git -c log.showSignature=false log -n 1 --pretty=format:%H
[        ] d79295af24c3ed621c33713ecda14ad196fd9c31
[        ] executing: [/Users/kpsroka/Workspace/SDK/flutter/] git tag --points-at d79295af24c3ed621c33713ecda14ad196fd9c31
[  +24 ms] Exit code 0 from: git tag --points-at d79295af24c3ed621c33713ecda14ad196fd9c31
[        ] 2.2.2
[   +5 ms] executing: [/Users/kpsroka/Workspace/SDK/flutter/] git rev-parse --abbrev-ref --symbolic @{u}
[   +7 ms] Exit code 0 from: git rev-parse --abbrev-ref --symbolic @{u}
[        ] origin/stable
[        ] executing: [/Users/kpsroka/Workspace/SDK/flutter/] git ls-remote --get-url origin
[   +6 ms] Exit code 0 from: git ls-remote --get-url origin
[        ] https://github.com/flutter/flutter.git
[  +92 ms] executing: [/Users/kpsroka/Workspace/SDK/flutter/] git rev-parse --abbrev-ref HEAD
[   +6 ms] Exit code 0 from: git rev-parse --abbrev-ref HEAD
[        ] stable
[   +6 ms] executing: sw_vers -productName
[  +15 ms] Exit code 0 from: sw_vers -productName
[        ] macOS
[        ] executing: sw_vers -productVersion
[  +13 ms] Exit code 0 from: sw_vers -productVersion
[        ] 11.4
[        ] executing: sw_vers -buildVersion
[  +13 ms] Exit code 0 from: sw_vers -buildVersion
[        ] 20F71
[  +59 ms] Artifact Instance of 'AndroidGenSnapshotArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'AndroidInternalBuildArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'IOSEngineArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'FlutterWebSdk' is not required, skipping update.
[   +2 ms] Artifact Instance of 'WindowsEngineArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'MacOSEngineArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'LinuxEngineArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'LinuxFuchsiaSDKArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'MacOSFuchsiaSDKArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'FlutterRunnerSDKArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'FlutterRunnerDebugSymbols' is not required, skipping update.
[  +52 ms] Artifact Instance of 'MaterialFonts' is not required, skipping update.
[        ] Artifact Instance of 'GradleWrapper' is not required, skipping update.
[        ] Artifact Instance of 'AndroidGenSnapshotArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'AndroidInternalBuildArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'IOSEngineArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'FlutterWebSdk' is not required, skipping update.
[        ] Artifact Instance of 'FlutterSdk' is not required, skipping update.
[        ] Artifact Instance of 'WindowsEngineArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'MacOSEngineArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'LinuxEngineArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'LinuxFuchsiaSDKArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'MacOSFuchsiaSDKArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'FlutterRunnerSDKArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'FlutterRunnerDebugSymbols' is not required, skipping update.
[        ] Artifact Instance of 'IosUsbArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'IosUsbArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'IosUsbArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'IosUsbArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'IosUsbArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'FontSubsetArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'PubDependencies' is not required, skipping update.
[  +12 ms] Using /Users/kpsroka/Workspace/SDK/flutter/.pub-cache for the pub cache.
[        ] executing: /Users/kpsroka/Workspace/SDK/flutter/bin/cache/dart-sdk/bin/pub run build_runner build --delete-conflicting-outputs --verbose
[INFO] Entrypoint:Generating build script...
[INFO] Entrypoint:Generating build script completed, took 528ms

[INFO] BuildDefinition:Initializing inputs
[INFO] BuildDefinition:Reading cached asset graph...
[INFO] BuildDefinition:Reading cached asset graph completed, took 410ms

[INFO] BuildDefinition:Checking for updates since last build...
[INFO] BuildDefinition:Checking for updates since last build completed, took 1.0s

[INFO] Build:Running build...
[INFO] Build:Running build completed, took 85ms

[INFO] Build:Caching finalized dependency graph...
[INFO] Build:Caching finalized dependency graph completed, took 214ms

[SEVERE] mockito:mockBuilder on test/test_test.dart (cached):

Null check operator used on a null value
package:mockito/src/builder.dart 1464:30                     _MockLibraryInfo._typeImport
package:mockito/src/builder.dart 1403:19                     _MockLibraryInfo._typeReference.<fn>
package:code_builder/src/specs/type_reference.g.dart 160:33  _$TypeReferenceBuilder.update
package:code_builder/src/specs/type_reference.g.dart 22:36   new _$TypeReference
package:mockito/src/builder.dart 1399:14                     _MockLibraryInfo._typeReference
package:mockito/src/builder.dart 1214:18                     _MockLibraryInfo._matchingParameter.<fn>
package:code_builder/src/specs/method.g.dart 641:33          _$ParameterBuilder.update
package:code_builder/src/specs/method.g.dart 403:32          new _$Parameter
package:mockito/src/builder.dart 1211:12                     _MockLibraryInfo._matchingParameter
package:mockito/src/builder.dart 967:18                      _MockLibraryInfo._buildOverridingMethod
package:mockito/src/builder.dart 1190:27                     _MockLibraryInfo._addFakeClass.<fn>.<fn>
package:code_builder/src/specs/method.g.dart 332:33          _$MethodBuilder.update
package:code_builder/src/specs/method.g.dart 38:29           new _$Method
package:mockito/src/builder.dart 1189:30                     _MockLibraryInfo._addFakeClass.<fn>
package:code_builder/src/specs/class.g.dart 292:33           _$ClassBuilder.update
package:code_builder/src/specs/class.g.dart 34:28            new _$Class
package:mockito/src/builder.dart 1163:21                     _MockLibraryInfo._addFakeClass
package:mockito/src/builder.dart 1150:9                      _MockLibraryInfo._dummyValueImplementing
package:mockito/src/builder.dart 1097:12                     _MockLibraryInfo._dummyValue
package:mockito/src/builder.dart 1000:24                     _MockLibraryInfo._buildOverridingMethod
package:mockito/src/builder.dart 887:36                      _MockLibraryInfo.methodOverrides.<fn>
package:code_builder/src/specs/method.g.dart 332:33          _$MethodBuilder.update
package:code_builder/src/specs/method.g.dart 38:29           new _$Method
package:mockito/src/builder.dart 887:15                      _MockLibraryInfo.methodOverrides
dart:core                                                    List.addAll
package:built_collection/src/list/list_builder.dart 98:14    ListBuilder.addAll
package:mockito/src/builder.dart 807:24                      _MockLibraryInfo._buildMockClass.<fn>
package:code_builder/src/specs/class.g.dart 292:33           _$ClassBuilder.update
package:code_builder/src/specs/class.g.dart 34:28            new _$Class
package:mockito/src/builder.dart 753:12                      _MockLibraryInfo._buildMockClass
package:mockito/src/builder.dart 743:23                      new _MockLibraryInfo
package:mockito/src/builder.dart 65:29                       MockBuilder.build

[SEVERE] Build:
Failed after 335ms
[+4723 ms] "flutter run" took 4,816ms.
[   +6 ms] pub finished with exit code 1
[   +1 ms] 
           #0      throwToolExit (package:flutter_tools/src/base/common.dart:10:3)
           #1      _DefaultPub.interactively (package:flutter_tools/src/dart/pub.dart:368:7)
           <asynchronous suspension>
           #2      PackagesForwardCommand.runCommand (package:flutter_tools/src/commands/packages.dart:241:5)
           <asynchronous suspension>
           #3      FlutterCommand.run.<anonymous closure> (package:flutter_tools/src/runner/flutter_command.dart:1043:27)
           <asynchronous suspension>
           #4      AppContext.run.<anonymous closure> (package:flutter_tools/src/base/context.dart:150:19)
           <asynchronous suspension>
           #5      CommandRunner.runCommand (package:args/command_runner.dart:196:13)
           <asynchronous suspension>
           #6      FlutterCommandRunner.runCommand.<anonymous closure> (package:flutter_tools/src/runner/flutter_command_runner.dart:284:9)
           <asynchronous suspension>
           #7      AppContext.run.<anonymous closure> (package:flutter_tools/src/base/context.dart:150:19)
           <asynchronous suspension>
           #8      FlutterCommandRunner.runCommand (package:flutter_tools/src/runner/flutter_command_runner.dart:232:5)
           <asynchronous suspension>
           #9      run.<anonymous closure>.<anonymous closure> (package:flutter_tools/runner.dart:62:9)
           <asynchronous suspension>
           #10     AppContext.run.<anonymous closure> (package:flutter_tools/src/base/context.dart:150:19)
           <asynchronous suspension>
           #11     main (package:flutter_tools/executable.dart:91:3)
           <asynchronous suspension>
           
           
[   +2 ms] Running shutdown hooks
[        ] Shutdown hooks complete
[   +1 ms] exiting with code 1

@it-sam
Copy link

it-sam commented Jul 14, 2021

Meanwhile we can exclude generated files.
Add below code to analysis_options.yaml

analyzer:
   exclude:
    - "**/*.g.dart"
    - "**/*.mocks.dart" 

@srawlins srawlins reopened this Jul 20, 2021
@srawlins
Copy link
Member

I think I have an additional fix for this, coming.

srawlins added a commit that referenced this issue Jul 20, 2021
* Properly reference types in overridden `toString` implementations.
  Fixes #438
* Override `toString` in a Fake implementation when the class-to-be-faked has
  a superclass which overrides `toString` with additional parameters.
  Fixes #371

PiperOrigin-RevId: 385852381
@tbm98
Copy link

tbm98 commented Jul 21, 2021

same here when generate a Widget

mosuem pushed a commit to dart-lang/test that referenced this issue Oct 17, 2024
…ch the

signature of an overriding method which adds optional parameters.

Fixes dart-lang/mockito#371

PiperOrigin-RevId: 377388828
mosuem pushed a commit to dart-lang/test that referenced this issue Oct 17, 2024
* Properly reference types in overridden `toString` implementations.
  Fixes dart-lang/mockito#438
* Override `toString` in a Fake implementation when the class-to-be-faked has
  a superclass which overrides `toString` with additional parameters.
  Fixes dart-lang/mockito#371

PiperOrigin-RevId: 385852381
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P2 A bug or feature request we're likely to work on type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)
Projects
None yet
Development

No branches or pull requests

5 participants