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

Support for generic type matching #95

Closed
mtwichel opened this issue Dec 19, 2021 · 6 comments
Closed

Support for generic type matching #95

mtwichel opened this issue Dec 19, 2021 · 6 comments
Labels
duplicate This issue or pull request already exists

Comments

@mtwichel
Copy link

Is your feature request related to a problem? Please describe.
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]

I am trying to make an object that supports the following type of method:

class Foo {
    T get<T>() {
        // implementation
    }
}

and I was hoping to mock it like so

class MockFoo extends Mock implements Foo { }

test('get returns proper values', () {
    final foo = MockFoo();
    when(() => foo.get<int>()).thenReturn(5);
    when(() => foo.get<String>()).thenReturn('test');

    expect(foo.get<int>(), 5);
    expect(foo.get<String>(), 'test');
});

Unfortunately, this throws the following error:
_TypeError (type 'String' is not a subtype of type 'int')

Describe the solution you'd like
It would be awesome if when mocking generics, Mocktail would be able to tell the difference between these two methods so the above test would pass.

Describe alternatives you've considered
I really don't think I could have an alternative solution without substantially changing how the object works.

Additional context
I believe the _useMatchedInvocationIfSet method (in lib/src/_is_invocation.dart, line 102) may be where a check that the invocation supports not only the same arguments but the same generic type definitions as well. In the passed invocation object, there is a field called _typeArguments that contains a list of the generic types passed into the invocation. I think we could add this as criteria for matching invocation and it would support this feature.

Thanks in advance! As always, I'm super grateful for everything you and this package bring to the Dart ecosystem, and I'm happy to help with a PR if you think this is worthwhile!

@mtwichel
Copy link
Author

Only now realizing this is probably a duplicate of #66 and fixed by #67. If it is I can close it and I'll drop a thumbs up on the PR :)

@felangel
Copy link
Owner

Hey @mtwichel yup it’s a duplicate and I’ll try to get that merged and published in the next few days 👍

@felangel felangel added the duplicate This issue or pull request already exists label Dec 19, 2021
@mtwichel
Copy link
Author

Thanks as always @felangel 🎉🎉

@c-lamont
Copy link

c-lamont commented Feb 15, 2022

@felangel @mtwichel I think the prs 66/67 were referring to fixing the verify issue:
final result = verify(() => stub.someMethodWithTypeArgs<SomeType>());

But I am still having the issue with when/return as described in the original question:
when(() => foo.get<int>()).thenReturn(5);

Could either of you confirm whether the when/return generic has been implemented or not?

@felangel
Copy link
Owner

@felangel @mtwichel I think the prs 66/67 were referring to fixing the verify issue: final result = verify(() => stub.someMethodWithTypeArgs<SomeType>());

But I am still having the issue with when/return as described in the original question: when(() => foo.get<int>()).thenReturn(5);

Could either of you confirm whether the when/return generic has been implemented or not?

Can you provide a link to a minimal reproduction sample? I'm happy to take a look 👍

@c-lamont
Copy link

So making a simplified project proved that Mocktail does handle generics correctly. From that I have managed to solve my issue 🎉
Thanks for the quick response and making this great package 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

3 participants