Reputation: 606
Before the null safety I could simply mock up the sendRequest(...) method like that:
void stubBaseRepositorySendRequestResponse(String response) {
when(baseRepository.sendRequest(onGetData: anyNamed('onGetData')))
.thenAnswer((_) {
return Future<String>.value(response);
});
}
where the signature of this method is:
Future<T> sendRequest<T>({required Future<T> Function() onGetData})
How can this be done with the new null-safe version of mockito? Can I still maintain the generic character of this stub and allow for any arguments to be passed in?
Upvotes: 13
Views: 9212
Reputation: 5182
Updating stubs after mockito null-safety upgrade:
To outline the process, a stubbed class will need to be generated, which is done using build_runner
package. You will need to import that class and stub required methods.
class MyClass extends Mock implements MyClassBase {}
instead of
class MockMyClass extends Mock implements MyClassBase {}
@GenerateMocks([MockedClass])
eg @GenerateMocks([MyClass])
(it'll require an import: import 'package:mockito/annotations.dart';
)flutter pub run build_runner build --delete-conflicting-outputs
import '{TEST_FILE_NAME}.mocks.dart';
. Now the stub class is available with the same name as given in Generate(
starting with Mock
, eg MockMyClass
when(mockMyClass.someFancyMethod(any, any))
.thenAnswer((_) => Future.value(null));
Was having issues mocking NavigatorObserver this way, the error I got:
The following MissingStubError was thrown building IconTheme(color: Color(0xdd000000)): 'navigator'
Stubbing navigator
with NavigatorState
didn't help, I guess it's related to context propagation.
I worked around it by using the following non-null safe way, as specified in mockito's null-safety guideline:
@GenerateMocks([],
customMocks: [
MockSpec<NavigatorObserver>(returnNullOnMissingStub: true)
])
after that run
flutter pub run build_runner build --delete-conflicting-outputs
It'll yield MockNavigatorObserver
available through the import of *.mocks.dart
file (mentioned above). Of course, since it's generated by mockito need to remove any custom definitions of that class.
Upvotes: 5
Reputation: 606
This can be easily done with mocktail. A lot easier, without the code gen required by mockito.
Important to import mocktail first:
import 'package:mocktail/mocktail.dart';
then create a mock of the class that we want to mock:
class MockBaseRepository extends Mock implements BaseRepository {}
void stubBaseRepositorySendRequestResponse(String response) {
when(() => baseRepository.sendRequest(
onGetData: any(named: 'onGetData', that: isNotNull))).thenAnswer((_) {
return Future<String>.value(response);
});
}
Upvotes: 14