I've seen numerous questions on StackOverflow from people trying to manually create a Mock for package:http's http.Client.
The Flutter cookbook page demonstrating mocking does exactly that, and I think it's steering people the wrong way. Mocking http.Client can be tricky since the stub might be called with a headers argument that they don't expect.
Furthermore, it's unnecessary. package:http provides its own MockClient class to make testing easier.
Expected fix
Create a different example that mocks something else.
Page URL
https://docs.flutter.dev/cookbook/testing/unit/mocking/
Page source
https://github.com/flutter/website/tree/main/src/cookbook/testing/unit/mocking.md
Describe the problem
I've seen numerous questions on StackOverflow from people trying to manually create a
Mock
forpackage:http
'shttp.Client
.The Flutter cookbook page demonstrating mocking does exactly that, and I think it's steering people the wrong way. Mocking
http.Client
can be tricky since the stub might be called with aheaders
argument that they don't expect.Furthermore, it's unnecessary.
package:http
provides its ownMockClient
class to make testing easier.Expected fix
Create a different example that mocks something else.
Additional context