0
0
Fluttermobile~10 mins

Mock dependencies in Flutter - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
Apackage:flutter_test/flutter_test.dart
Bpackage:flutter/material.dart
Cpackage:mockito/mockito.dart
Dpackage:test/test.dart
Attempts:
3 left
💡 Hint
Common Mistakes
Importing flutter_test instead of mockito.
Using the material package import by mistake.
2fill in blank
medium

Complete 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'
AFake
BMock
CWidget
DStatelessWidget
Attempts:
3 left
💡 Hint
Common Mistakes
Extending Fake instead of Mock.
Trying to extend Widget or StatelessWidget.
3fill in blank
hard

Fix 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'
AregisterSingleton
BregisterFactory
CregisterLazySingleton
DregisterProvider
Attempts:
3 left
💡 Hint
Common Mistakes
Using registerLazySingleton which expects a factory function.
Using registerFactory which creates new instances every time.
4fill in blank
hard

Fill 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'
AgetData
BfetchData
CFuture.value('Hello')
DFuture.delayed(Duration(seconds: 1), () => 'Hello')
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong method name like fetchData.
Returning a plain string instead of a Future.
5fill in blank
hard

Fill 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'
AgetData
BfetchData
C1
D2
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong method name in verify.
Using called(2) when only one call was made.