Book gives:
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
Errors:
Key
to optional: Key?
required
before this.title
Dart null safety means you have to use generated code for mocking now.
import 'package:flutter_test/flutter_test.dart';
import 'package:http/http.dart' as http;
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'package:xckd_app/main.dart';
import 'dart:io';
@GenerateNiceMocks([MockSpec<http.Client>(), MockSpec<File>()])
import 'unit_test.mocks.dart';
const comics = [/* ... */];
void main() {
test("get latest comic number", () async {
var latestComicNumberFile = MockFile();
var latestComicNumberExists = false;
when(mockHttp.read(Uri.parse('https://xkcd.com/info.0.json')))
.thenAnswer((_) {
return Future.value(comics[1]);
});
expect(
await getLatestComicNumber(
httpClient: mockHttp,
latestComicNFile: latestComicNumberFile,
),
2,
);
});
}