text stringlengths 1 372 |
|---|
sometimes, unit tests might depend on classes that fetch data from live |
web services or databases. this is inconvenient for a few reasons: |
therefore, rather than relying on a live web service or database, |
you can “mock” these dependencies. mocks allow emulating a live |
web service or database and return specific results depending |
on the situation. |
generally speaking, you can mock dependencies by creating an alternative |
implementation of a class. write these alternative implementations by |
hand or make use of the mockito package as a shortcut. |
this recipe demonstrates the basics of mocking with the |
mockito package using the following steps: |
for more information, see the mockito package documentation. |
<topic_end> |
<topic_start> |
1. add the package dependencies |
to use the mockito package, add it to the |
pubspec.yaml file along with the flutter_test dependency in the |
dev_dependencies section. |
this example also uses the http package, |
so define that dependency in the dependencies section. |
mockito: 5.0.0 supports dart’s null safety thanks to code generation. |
to run the required code generation, add the build_runner dependency |
in the dev_dependencies section. |
to add the dependencies, run flutter pub add: |
<topic_end> |
<topic_start> |
2. create a function to test |
in this example, unit test the fetchAlbum function from the |
fetch data from the internet recipe. |
to test this function, make two changes: |
the function should now look like this: |
<code_start> |
Future<Album> fetchAlbum(http.Client client) async { |
final response = await client |
.get(uri.parse('https://jsonplaceholder.typicode.com/albums/1')); |
if (response.statuscode == 200) { |
// if the server did return a 200 OK response, |
// then parse the JSON. |
return Album.fromJson(jsonDecode(response.body) as Map<String, dynamic>); |
} else { |
// if the server did not return a 200 OK response, |
// then throw an exception. |
throw Exception('Failed to load album'); |
} |
} |
<code_end> |
in your app code, you can provide an http.Client to the fetchAlbum method |
directly with fetchAlbum(http.Client()). http.Client() creates a default |
http.Client. |
<topic_end> |
<topic_start> |
3. create a test file with a mock http.Client |
next, create a test file. |
following the advice in the introduction to unit testing recipe, |
create a file called fetch_album_test.dart in the root test folder. |
add the annotation @generatemocks([http.client]) to the main |
function to generate a MockClient class with mockito. |
the generated MockClient class implements the http.Client class. |
this allows you to pass the MockClient to the fetchAlbum function, |
and return different http responses in each test. |
the generated mocks will be located in fetch_album_test.mocks.dart. |
import this file to use them. |
<code_start> |
import 'package:http/http.dart' as http; |
import 'package:mocking/main.dart'; |
import 'package:mockito/annotations.dart'; |
// generate a MockClient using the mockito package. |
// create new instances of this class in each test. |
@generatemocks([http.client]) |
void main() { |
} |
<code_end> |
next, generate the mocks running the following command: |
<topic_end> |
<topic_start> |
4. write a test for each condition |
the fetchAlbum() function does one of two things: |
therefore, you want to test these two conditions. |
use the MockClient class to return an “ok” response |
for the success test, and an error response for the unsuccessful test. |
test these conditions using the when() function provided by |
mockito: |
<code_start> |
import 'package:flutter_test/flutter_test.dart'; |
import 'package:http/http.dart' as http; |
import 'package:mocking/main.dart'; |
import 'package:mockito/annotations.dart'; |
import 'package:mockito/mockito.dart'; |
import 'fetch_album_test.mocks.dart'; |
// generate a MockClient using the mockito package. |
// create new instances of this class in each test. |
@generatemocks([http.client]) |
void main() { |
group('fetchAlbum', () { |
test('returns an album if the http call completes successfully', () async { |
final client = MockClient(); |
// use mockito to return a successful response when it calls the |
// provided http.Client. |
when(client |
.get(uri.parse('https://jsonplaceholder.typicode.com/albums/1'))) |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.