In this lesson we'll cover:
flutter_test
packageexpect()
, isNotNull()
, equals()
etcYou can check out the step/step08
branch here which will contain the code for this lesson.
flutter_test
dependency existsdev_dependencies:
flutter_test: any
test
directorytests/widget_test.dart
file, we'll create it later as it's not needed right now.tests/unit/location_test.dart
fetchAll()
Method// test/unit/location_test.dart
import 'package:flutter_test/flutter_test.dart';
import 'package:tourismandco/models/location.dart';
void main() {
test('Locations can be fetched', () {
final locations = Location.fetchAll();
expect(locations.length, greaterThan(0));
});
// NOTE we'll add more tests here later in this lesson
}
Location.fetchAll()
.flutter test test/<your test file>
so in our case flutter test test/unit/location_test.dart
We still should test our individual widgets, as well as an entire app 'walkthrough' where the tests run the app and tap on the important parts to ensure they're working. We'll cover that next.
fetchByID()
Method// test/unit/location_test.dart
// ...
test('Locations can be fetched by ID', () {
final locations = Location.fetchAll();
for (Location l in locations) {
final fetchedLocation = Location.fetchByID(l.id);
expect(fetchedLocation, isNotNull);
expect(fetchedLocation.id, equals(l.id));
}
});
That's it! Very simple but we'll expand on this by adding widget and integration tests.