You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
@overrideIterable<E> get values => (super.noSuchMethod(Invocation.getter(#values), returnValue: []) asIterable<E>);
Sample test code
@GenerateMocks([HiveInterface, Box])
voidmain() async {
// other codes herefinal userList = [User()];
test(
'should return user if the get request succeed',
() async {
//arrangewhen(hive.box(any)).thenReturn(box);
when(box.values).thenReturn(userList);
//actfinal result =await repository.getAll();
//assetverify(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()
@overrideIterable<E> get values => (super.noSuchMethod(Invocation.getter(#values), returnValue:<E>[]) asIterable<E>);
or
@overrideIterable<E> get values => (super.noSuchMethod(Invocation.getter(#values), returnValue:Iterable<E>.empty()) asIterable<E>);
Version
Platform: macOs Big Sur, 11.4
Flutter version: Channel stable, 2.2.2
Mockito version: 5.0.10
The text was updated successfully, but these errors were encountered:
Shashindran
changed the title
Mock generator returns array for Iterable
Mock generator maps incorrectly for Iterable returning method
Jul 12, 2021
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
…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
…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.
Fixesdart-lang/mockito#445
PiperOrigin-RevId: 384965296
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
Sample test code
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: []
toreturnValue: <E>[]
orreturnValue: Iterable<E>.empty()
or
Version
The text was updated successfully, but these errors were encountered: