Skip to content

Cannot generate mock of class which mixes in DiagnosticableTreeMixin #400

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

Closed
matejthetree opened this issue Apr 29, 2021 · 14 comments
Closed

Comments

@matejthetree
Copy link

Since the recommended way is to code generate mocks, is there a way to use code generation with the fix mentioned here?
#233

@matejthetree
Copy link
Author

matejthetree commented Apr 29, 2021

to test this, try to generate mocks for this class

class TestFor with DiagnosticableTreeMixin {}

@matejthetree
Copy link
Author

#397 A question for code generation was asked here as well, but the answer pointed to manual implementation.

@matejthetree
Copy link
Author

matejthetree commented May 3, 2021

thinking about this, if we could override the generated class somehow to make this work, or maybe use it as a mixin.

what do you guys think?

@srawlins
Copy link
Member

srawlins commented May 3, 2021

@matejthetree This issue's description is a mystery to me. I'm not sure what problem or question you have. I'm not sure what "diagnostics problem" means.

In reference to the issues about overriding toString with additional parameters, I have an unrelated proposal to allow one to mix in a mixin: #342. Not sure if it would solve this issue...

@matejthetree
Copy link
Author

@srawlins thanks for the proposal. It might work by creating the mixin that overrides to String.

The issue can be easily reproduced by trying to generate mock for
class Test with DiagnosticableTreeMixin {}

Looking forward to your updates

@srawlins
Copy link
Member

srawlins commented Jun 1, 2021

Sorry for the delay @matejthetree. I do't know what DiagnosticableTreeMixin is. Is this a class you wrote? I'll need a self-contained minimal reproduction in order to debug.

@matejthetree
Copy link
Author

It is a flutter debug class that is implemented if you want your widget to have custom debug data in the flutter inspection (dev tools). It is used a lot when writing frameworks, to provide details about the state from the inspector itself.

I will make a simple app that you can clone.

@matejthetree
Copy link
Author

here it is.

Write this test with the latest version of mockito.

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

@GenerateMocks([Test])
void main() {}

class Test with DiagnosticableTreeMixin {}

The file created will have this error:

'Object.toString' ('String Function()') isn't a valid concrete implementation of 'DiagnosticsNode.toString' ('String Function({DiagnosticLevel minLevel, TextTreeConfiguration? parentConfiguration})').

I have to go and create manual mocks for huge classes, and this makes it a bit hard to test :)

it would be nice If I could define a mixin that would handle this problematic function that I have in all my business logic.

@srawlins
Copy link
Member

srawlins commented Jun 2, 2021

Ah yes. Similar to #371, but that is for Fakes. This is for Mocks.

@srawlins srawlins changed the title diagnostics problem and code generation? Cannot generate mock of class which mixes in DiagnosticableTreeMixin Jun 2, 2021
@srawlins
Copy link
Member

srawlins commented Jun 2, 2021

I suspect this is fixed in 5.0.9, but I'm not certain. I cannot reproduce this at HEAD.

@matejthetree
Copy link
Author

great. Let me try with latest git release

@matejthetree
Copy link
Author

This is in fact the fake error.

Here is the output I get from latest git mockito.

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

/// A class which mocks [Test].
///
/// See the documentation for Mockito's code generation for more information.
class MockTest extends _i1.Mock implements _i3.Test {
  MockTest() {
    _i1.throwOnMissingStub(this);
  }

  @override
  String toString({_i2.DiagnosticLevel? minLevel = _i2.DiagnosticLevel.info}) =>
      super.toString();
  @override
  String toStringShallow(
          {String? joiner = r', ',
          _i2.DiagnosticLevel? minLevel = _i2.DiagnosticLevel.debug}) =>
      (super.noSuchMethod(
          Invocation.method(
              #toStringShallow, [], {#joiner: joiner, #minLevel: minLevel}),
          returnValue: '') as String);
  @override
  String toStringDeep(
          {String? prefixLineOne = r'',
          String? prefixOtherLines,
          _i2.DiagnosticLevel? minLevel = _i2.DiagnosticLevel.debug}) =>
      (super.noSuchMethod(
          Invocation.method(#toStringDeep, [], {
            #prefixLineOne: prefixLineOne,
            #prefixOtherLines: prefixOtherLines,
            #minLevel: minLevel
          }),
          returnValue: '') as String);
  @override
  String toStringShort() => (super
          .noSuchMethod(Invocation.method(#toStringShort, []), returnValue: '')
      as String);
  @override
  _i2.DiagnosticsNode toDiagnosticsNode(
          {String? name, _i2.DiagnosticsTreeStyle? style}) =>
      (super.noSuchMethod(
          Invocation.method(
              #toDiagnosticsNode, [], {#name: name, #style: style}),
          returnValue: _FakeDiagnosticsNode()) as _i2.DiagnosticsNode);
  @override
  List<_i2.DiagnosticsNode> debugDescribeChildren() =>
      (super.noSuchMethod(Invocation.method(#debugDescribeChildren, []),
          returnValue: <_i2.DiagnosticsNode>[]) as List<_i2.DiagnosticsNode>);
  @override
  void debugFillProperties(_i2.DiagnosticPropertiesBuilder? properties) =>
      super.noSuchMethod(Invocation.method(#debugFillProperties, [properties]),
          returnValueForMissingStub: null);
}

End the error is indeed on

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

@matejthetree
Copy link
Author

matejthetree commented Jun 3, 2021

I tried to do it with new API but with no luck

@GenerateMocks([
  Test
], customMocks: [
  MockSpec<Test>(as: #TestCustom, fallbackGenerators: {#toString: toString})
])
String toString(
    {TextTreeConfiguration? parentConfiguration,
    DiagnosticLevel? minLevel = DiagnosticLevel.info}) {
  return '';
}

I still get the same Fake error

@srawlins
Copy link
Member

srawlins commented Jun 3, 2021

OK this is a duplicate of #371 then.

@srawlins srawlins closed this as completed Jun 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants