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

Mock generator maps incorrectly for method returning Iterable with type parameter #445

Closed
Shashindran opened this issue Jul 12, 2021 · 1 comment
Assignees

Comments

@Shashindran
Copy link

Shashindran commented Jul 12, 2021

Steps to Reproduce
Generate a mock for class with a getter which returns an Iterable.

For my scenario, I mocked hive box class which its values method return an Iterable.

The generated mock method as below

@override
Iterable<E> get values => (super.noSuchMethod(Invocation.getter(#values), returnValue: []) as Iterable<E>);

Sample test code

@GenerateMocks([HiveInterface, Box])
void main() async {

// other codes here

 final userList = [User()];

 test(
   'should return user if the get request succeed',
   () async {
     //arrange
     when(hive.box(any)).thenReturn(box);
     when(box.values).thenReturn(userList);

     //act
     final result = await repository.getAll();

     //asset
     verify(box.values).called(1);
     verifyNoMoreInteractions(box);
     expect(result, userList);
   },
 );

Outcome
Running test with the mocked method gives the following error
type 'List<dynamic>' is not a subtype of type 'Iterable<User>' in type cast MockBox.values

Expected
Test should pass

Possible solution
Able to make the error go away by changing the returnValue: [] to returnValue: <E>[] or returnValue: Iterable<E>.empty()

@override
Iterable<E> get values => (super.noSuchMethod(Invocation.getter(#values), returnValue: <E>[]) as Iterable<E>);

or

@override
Iterable<E> get values => (super.noSuchMethod(Invocation.getter(#values), returnValue: Iterable<E>.empty()) as Iterable<E>);

Version

  • Platform: macOs Big Sur, 11.4
  • Flutter version: Channel stable, 2.2.2
  • Mockito version: 5.0.10
@Shashindran Shashindran changed the title Mock generator returns array for Iterable Mock generator maps incorrectly for Iterable returning method Jul 12, 2021
@Shashindran Shashindran changed the title Mock generator maps incorrectly for Iterable returning method Mock generator maps incorrectly for method returning Iterable with type parameter Jul 12, 2021
@srawlins
Copy link
Member

Thanks for the bug report. I've got a fix in the works.

@srawlins srawlins self-assigned this Jul 15, 2021
srawlins added a commit that referenced this issue Jul 18, 2021
…e<T>`.

Before this fix, we would only return `[]` which had an implicit dynamic: `<dynamic>[]`. The proper return value should explicitly include the expected type argument.

Existing test cases cover the desired behavior.

Also replace `var` with `final` in surrounding code.

Fixes #445

PiperOrigin-RevId: 384965296
mosuem pushed a commit to dart-lang/test that referenced this issue Oct 17, 2024
…e<T>`.

Before this fix, we would only return `[]` which had an implicit dynamic: `<dynamic>[]`. The proper return value should explicitly include the expected type argument.

Existing test cases cover the desired behavior.

Also replace `var` with `final` in surrounding code.

Fixes dart-lang/mockito#445

PiperOrigin-RevId: 384965296
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