Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import the mockito package for mocking dependencies in Flutter tests.
Flutter
import '[1]';
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Importing flutter_test instead of mockito.
Using the material package import by mistake.
✗ Incorrect
The mockito package is imported with 'package:mockito/mockito.dart' to create mock classes for testing.
2fill in blank
mediumComplete the code to create a mock class for a dependency named ApiService.
Flutter
class MockApiService extends [1] implements ApiService {}
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Extending Fake instead of Mock.
Trying to extend Widget or StatelessWidget.
✗ Incorrect
Mockito requires mock classes to extend the Mock class to provide mocking behavior.
3fill in blank
hardFix the error in the test setup by completing the code to register the mock ApiService with the dependency injector.
Flutter
setUp(() {
[1](MockApiService());
}); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using registerLazySingleton which expects a factory function.
Using registerFactory which creates new instances every time.
✗ Incorrect
registerSingleton registers the provided mock instance directly as a singleton, suitable for test setups.
4fill in blank
hardFill both blanks to stub the getData method of the mock ApiService to return a Future with 'Hello'.
Flutter
when(mockApiService.[1]()).thenAnswer((_) async => [2]);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong method name like fetchData.
Returning a plain string instead of a Future.
✗ Incorrect
The method getData is stubbed to return a Future with the string 'Hello' using Future.value.
5fill in blank
hardFill both blanks to verify that the getData method was called exactly once on the mock ApiService.
Flutter
verify(mockApiService.[1]()).called([2]);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong method name in verify.
Using called(2) when only one call was made.
✗ Incorrect
The verify function checks that getData was called exactly once using called(1).