You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Details:
Sometimes, we need to provide an errorBuilder for Image.network, and, at least so far, I didn't see a way to test it without mocking httpClient, which is basically what mockNetworkImages already does.
Expected:
mockNetworkImages should have a new option to make the request fail as failed to fetch the image, so Image.Network can use the errorBuilder instead.
I would really appreciate it if someone had a better and simpler way to do it.
The text was updated successfully, but these errors were encountered:
DanielFerrariR
changed the title
Mock Image Network should provide a way to test the errorBuilder from Image.network
mockImageNetwork should provide a way to test the errorBuilder from Image.network
Oct 5, 2023
DanielFerrariR
changed the title
mockImageNetwork should provide a way to test the errorBuilder from Image.network
mockNetworkImages should provide a way to test the errorBuilder from Image.network
Oct 5, 2023
I have implemented the following approach to simulate an image load failure and verify that the appropriate error message and icon are displayed when the image fails to load:
// A mock class for simulating HTTP client behavior during testingclassMockHttpClientextendsMockimplementsHttpClient {}
...
testWidgets('displays error message when image fails to load',
(WidgetTester tester) async {
// Simulate a failure in retrieving the image by throwing an exceptionwhen(
() => httpClient.getUrl(
Uri.parse(
'https://image.tmdb.org/t/p/w500/invalid_path.jpg',
),
),
).thenThrow(Exception());
// Use HttpOverrides to replace the default HTTP client with our mocked clientawaitHttpOverrides.runZoned(
() async {
// Build the widget with an invalid image path to simulate a failed loadawait tester.pumpWidget(
constMaterialApp(
home:Scaffold(
body:TMDBImage(
path:'/invalid_path.jpg', // Invalid path to trigger errorBuilder
width:150,
height:225,
),
),
),
);
// Simulate a frame to allow the image to attempt loadingawait tester.pump();
// Verify that the error icon and message are displayed when the load failsexpect(find.text('Error'), findsOneWidget); // Check if "Error" text appearsexpect(find.byIcon(Icons.error_outline), findsOneWidget); // Check if error icon appears
},
createHttpClient: (_) => httpClient, // Replace default client with mock
);
});
Details:
Sometimes, we need to provide an errorBuilder for Image.network, and, at least so far, I didn't see a way to test it without mocking httpClient, which is basically what mockNetworkImages already does.
Expected:
mockNetworkImages should have a new option to make the request fail as failed to fetch the image, so Image.Network can use the errorBuilder instead.
I would really appreciate it if someone had a better and simpler way to do it.
The text was updated successfully, but these errors were encountered: