I have a method that I would like to mock, however when I am trying to verify calls of this method. I get an error that says:
Used on a non-mockito object
Here is the simplified code:
test('test',() {
MockReducer reducer = new MockReducer();
verify(reducer).called(0);
});
class MockReducer extends Mock {
call(state, action) => state;
}
Why can't I do something like this?
I think you have three problems here:
redux.dart
, you can implement the ReducerClass
(which acts as a Reducer
function by implementing call
).verifyNever
instead of verify(X).called(0)
.Working example:
class MockReducer extends Mock implements ReducerClass {}
main() {
test('should be able to mock a reducer', () {
final reducer = new MockReducer();
verifyNever(reducer.call(any, any));
});
}
If you want to just create a mocked function to use as onTap on widget test environment, with mockito you can do something like that:
abstract class MyFunction {
void call();
}
class MyFunctionMock extends Mock implements MyFunction {}
Test:
void main() {
final mock = MyFunctionMock();
...
// This is inside the the testWidgets callback
await tester.pumpWidget(
widgetToBeTested(
item: registeredListItem,
onTap: mock,
),
);
//Logic to tap the widgetToBeTested
verify(mock()).called(1);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With