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

thenAnswer Invocation with callback positional parameter does not handle non-primitives when compiled with dart2js #198

Open
smaifullerton-wk opened this issue Jun 20, 2019 · 4 comments
Labels
P2 A bug or feature request we're likely to work on S2 status-blocked Blocked from making progress by another (referenced) issue type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)

Comments

@smaifullerton-wk
Copy link

Here is a simplified test case of a bug I'm seeing with the thenAnswer Invocation when this code is compiled with dart2js. With DDC, it works as expected:

import 'package:test/test.dart';
import 'package:mockito/mockito.dart';

void main() {
  group('MockBassetHound', () {
    MockBassetHound bassetHound;

    setUp(() {
      bassetHound = MockBassetHound();
    });

    test('demonstrates bug', () {
      when(bassetHound.takesACallback(any)).thenAnswer((Invocation inv) {
        var callback = inv.positionalArguments[0];
        callback(new BassetPayload('Gerald'));
      });

      var callbackParam = ((BassetPayload payload) {
        print(payload == null); // false
        print(payload.runtimeType); // BassetPayload

        // NoSuchMethodError: method not found: 'get$name' (thing.get$name is not a function)
        expect(payload.name, 'Gerald');
      });

      bassetHound.takesACallback(callbackParam);
    });
  });
}

class MockBassetHound extends Mock implements BassetHound {}

class BassetHound {
  void takesACallback(Function cb) {}
}

class BassetPayload {
  final String name;

  BassetPayload(this.name);
}

There are no issues if the callback payload is a primitive.

It appears that Invocation with a callback argument works correctly on its own with this simplified case:

void main() async {
  var cb = (Rotsky rotsky) => print(rotsky.name);
  var inv = new Invocation.method(new Symbol('printName'), [cb]);
  
  inv.positionalArguments[0](new Rotsky('Lucy')); // 'Lucy'
}

class Rotsky {
  final String name;
  
  Rotsky(this.name);
}

Thank you for taking a look.

@srawlins
Copy link
Member

First of all, I have to thank you, so so much, on a stellar bug report. 👏 👏 👏 Thank you for the craftsmanship in the minimal reproduction, for the thoughtful embedded debug statements, and for the note about dart2js vs DDC. I can't guess at how long you've been trying to figure this problem out, in order to ultimately come up with this detailed report.

I've been able to reproduce this bug exactly (DDC is happy, dart2js is not). I'll look into it; seems like it may be a dart2js bug...

@srawlins
Copy link
Member

I believe this is a dart2js bug. I've filed dart-lang/sdk#37322.

@srawlins srawlins added P2 A bug or feature request we're likely to work on S2 status-blocked Blocked from making progress by another (referenced) issue type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) labels Jul 16, 2019
@travissanderson-wf
Copy link

@srawlins any update on when this might be looked at? the dart2js issue has no movement on it as of yet

@srawlins
Copy link
Member

No fix on dart2js yet, but we just found a cause and workaround at dart-lang/sdk#37322 (comment).

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 S2 status-blocked Blocked from making progress by another (referenced) issue type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)
Projects
None yet
Development

No branches or pull requests

3 participants