-
Notifications
You must be signed in to change notification settings - Fork 173
Closed
Description
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
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels