Can't test image widget from asset built with Image.asset() constructor in Flutter

404 Views Asked by At

I have an image in the sign in screen in my flutter app. It's basically built as

return Scaffold(
home: Column(
children:[
//This is a custom button 
 SocialMediaSignInButton(
              text: 'Sign in with Google',
              icon: Image.asset(
                'assets/google_image.png',
                scale: 1.8,
              ),
              onTap: (){},
            ),
],
),
);


//the social media sign in button is basically:
class SocialMediaSignInButton extends StatelessWidget {
  final String text;
  final Widget icon;
  final Function()? onTap;
  const SocialMediaSignInButton({
    super.key,
    required this.text,
    required this.icon,
    this.onTap,
  });

  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: onTap,
      borderRadius: BorderRadius.circular(6.0),
      child: Padding(
        padding: const EdgeInsets.all(8.0),
        child: SizedBox(
          width: 200.0,
          child: Row(
            children: [
              icon,
              const Gap(4.0),
              Text(text),
            ],
          ),
        ),
      ),
    );
  }
}

So I have been trying to write a test to check that the correct image is loaded in the button but the test keeps failing.

These are the tests That I have attempted that failed:

 testWidgets(
      'Testing the Sign in page Button Images',
      (widgetTester) async {
//This screen is the scaffold widget.
        await widgetTester.pumpWidget(const SignInScreen()));

//This failed
        expect(find.image(const AssetImage('assets/google_image.png')),
            findsOneWidget);

//This failed
expect(
       find.widgetWithImage(Image,
                const AssetImage('assets/google_image.png')),
        findsOneWidget);

      },
    );

//This passes
 expect(find.byType(Image), findsOneWidget);

The failed scenarios says that no image is found with the source 'assets/google_image.png' so I can test that an Image is present but I can't test the source url of the image.

0

There are 0 best solutions below