text stringlengths 1 372 |
|---|
} |
} |
<code_end> |
<topic_end> |
<topic_start> |
test/fetch_album_test.dart |
<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'))) |
.thenanswer((_) async => |
http.Response('{"userId": 1, "id": 2, "title": "mock"}', 200)); |
expect(await fetchAlbum(client), isA<Album>()); |
}); |
test('throws an exception if the http call completes with an error', () { |
final client = MockClient(); |
// use mockito to return an unsuccessful response when it calls the |
// provided http.Client. |
when(client |
.get(uri.parse('https://jsonplaceholder.typicode.com/albums/1'))) |
.thenanswer((_) async => http.Response('Not found', 404)); |
expect(fetchAlbum(client), throwsException); |
}); |
}); |
} |
<code_end> |
<topic_end> |
<topic_start> |
summary |
in this example, you’ve learned how to use mockito to test functions or classes |
that depend on web services or databases. this is only a short introduction to |
the mockito library and the concept of mocking. for more information, |
see the documentation provided by the mockito package. |
<topic_end> |
<topic_start> |
an introduction to widget testing |
in the introduction to unit testing recipe, |
you learned how to test dart classes using the test package. |
to test widget classes, you need a few additional tools provided by the |
flutter_test package, which ships with the flutter SDK. |
the flutter_test package provides the following tools for |
testing widgets: |
if this sounds overwhelming, don’t worry. learn how all of these pieces fit |
together throughout this recipe, which uses the following steps: |
<topic_end> |
<topic_start> |
1. add the flutter_test dependency |
before writing tests, include the flutter_test |
dependency in the dev_dependencies section of the pubspec.yaml file. |
if creating a new flutter project with the command line tools or |
a code editor, this dependency should already be in place. |
<topic_end> |
<topic_start> |
2. create a widget to test |
next, create a widget for testing. for this recipe, |
create a widget that displays a title and message. |
<code_start> |
class MyWidget extends StatelessWidget { |
const MyWidget({ |
super.key, |
required this.title, |
required this.message, |
}); |
final string title; |
final string message; |
@override |
widget build(BuildContext context) { |
return MaterialApp( |
title: 'flutter demo', |
home: scaffold( |
appBar: AppBar( |
title: text(title), |
), |
body: center( |
child: text(message), |
), |
), |
); |
} |
} |
<code_end> |
<topic_end> |
<topic_start> |
3. create a testWidgets test |
with a widget to test, begin by writing your first test. |
use the testWidgets() function provided by the |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.