'Flutter mock http.Client always return NUll
On Flutter unit testing i am following the same tutorial from https://docs.flutter.dev/cookbook/testing/unit/mocking , but when try to run test . The http.Client always return null. how to solve this? Generated Mock Client
@GenerateMocks([
http.Client
], customMocks: [
MockSpec<NavigationService>(returnNullOnMissingStub: true),
MockSpec<UserService>(returnNullOnMissingStub: true),
MockSpec<DialogService>(returnNullOnMissingStub: true),
MockSpec<AlbumService>(returnNullOnMissingStub: true)
])
test('returns an Album if the http call completes successfully', () async {
final client = MockClient();
final albumService = getAndRegisterAlbumService();
when(client
.get(Uri.parse('https://jsonplaceholder.typicode.com/albums/1')))
.thenAnswer((_) async =>
http.Response('{"userId": 1, "id": 2, "title": "mock"}', 200));
// expect(await albumService.fetchAlbum(client), isA<Album>());
expect(await albumService.fetchAlbum(client), isA<Album>());
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
