integration test with AlertDialog after a Future function

44 views Asked by At

I'm facing an issue with my Flutter integration test, and I could use some guidance on how to resolve it.

I have a Flutter app where I'm trying to test the display of a dialog when a button is tapped. The button callback awaits a Future Function and, after that, displays a message. I would like to make a integration test to check if the display was showed up. I tried to create the simplest version of the problem.

import 'package:flutter/material.dart';


void main() {
  runApp(const MyApp());
}

Future<void> showMessage(String msg,BuildContext context) async{
  await showDialog(
    context: context, 
    builder: (context) {
      return AlertDialog(
        content: Text(msg),
        actions: [
          TextButton(
            onPressed: () {
              Navigator.pop(context);
            },
            child:const Text("Ok")
          )
        ],
      );
    },
  );
}
class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      title: 'Flutter Demo',
      home:  MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  final String title;
  const MyHomePage({super.key, required this.title});
  
  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  Future<int> awaitNumber() async {
    return Future<int>.delayed(const Duration(milliseconds: 500),()=>3);
  
  }

  @override
  Widget build(BuildContext context) {
    
    return Scaffold(
      appBar: AppBar(
        
        title: Text(widget.title),
      ),
      body: TextButton(
        key:const Key("button"),
        onPressed: () async{
          awaitNumber().then((result) {
            showMessage("Number:$result", context);
          });
          
        },
        child:const Text("Button"),
        ),
      
    );
  }
}

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:debug_alert_dialog/main.dart';

main(){
  testWidgets('Counter increments smoke test', (WidgetTester tester) async {
    // Build our app and trigger a frame.
    await tester.pumpWidget(const MyApp());
    var button = find.byKey(const Key("button"));
    expect(button, findsOneWidget);
    await tester.runAsync(() async{
      await tester.tap(button);
    });
    await tester.pumpAndSettle();
    var text= find.text("Number:3");
    expect(text, findsWidgets);
    
  });
}

I saw some posts on Stack Overflow, but anyone help me out.I would like to fix my integration test

1

There are 1 answers

0
geneilson freire On

a little change solved my problem

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:debug_alert_dialog/main.dart';

main(){
  testWidgets('Counter increments smoke test', (WidgetTester tester) async {
    // Build our app and trigger a frame.
    await tester.pumpWidget(const MyApp());
    var button = find.byKey(const Key("button"));
    expect(button, findsOneWidget);
    
    await tester.runAsync(() async{
      
      await tester.tap(button);
      await Future.delayed(const Duration(seconds: 2));
      
      await tester.pumpAndSettle();
    });
    
    
    await tester.pumpAndSettle(const Duration(seconds: 2));
    
    var text = find.text("Number:3");
    expect(text, findsWidgets);
    
  });
}