如何在flutter中测试showDialog内容?

问题描述

有没有办法测试显示对话框中的内容? 我在项目中尝试做BDD,场景如下:

As a User,I would like to add a photo or select one from the gallery so that I can use it on the item.

以下是我用来测试的代码,但由于某种原因,测试失败。

add_item_view.dart

import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:my_app_mobile/models/graded_item/graded_item.dart';
import 'package:my_app_mobile/template/index.dart' as template;
import 'package:image_picker/image_picker.dart';

class AddItemView extends HookWidget {
  final GradedItem gradedItem;
  static final Key photoKey = Key('#photoKey');
  static final GlobalKey<FormState> formKey = GlobalKey<FormState>();
  final void Function() onPhoto;
  final ImagePicker _imagePicker = ImagePicker();

  AddItemView({
    @required this.gradedItem,this.onPhoto,});

  @override
  Widget build(BuildContext context) {
    final _image = useState<File>();

    Future getImage() async {
      final pickedFile = await _imagePicker.getImage(source: ImageSource.camera);

      if (pickedFile != null) {
        _image.value = File(pickedFile.path);
      } else {
        print('No image selected.');
      }
    }

    return Scaffold(
      appBar: AppBar(),body: SingleChildScrollView(
        child: Form(
          key: formKey,child: Column(
            children: [
              GestureDetector(
                onTap: () async {
                  if (onPhoto != null) {
                    onPhoto();
                  }
                  showDialog(
                    context: context,barrierColor: Colors.red.withOpacity(.2),builder: (context) {
                      return CameraOptions();
                    },);
                },child: Container(
                  key: photoKey,alignment: Alignment.center,child: Icon(
                    Icons.add_a_photo,color: Theme.of(context).primaryColor,size: 44.0,),height: 100.0,width: 100.0,decoration: BoxDecoration(
                    color: template.colors.grey340,borderRadius: BorderRadius.circular(10.0),],);
  }
}

class CameraOptions extends StatelessWidget {
  static final Key captureButtonPhotoKey = Key('#captureButtonPhotoKey');
  static final Key chooseButtonPhotoKey = Key('#chooseButtonPhotoKey');
  static final Key cancelButtonKey = Key('#cancelButtonKey');

  final void Function() onCapture;
  final void Function() onChoose;
  final void Function() onCancel;

  CameraOptions({this.onCapture,this.onChoose,this.onCancel});

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Container(
        height: 200.0,width: 200.0,child: Icon(Icons.camera,color: Colors.blue),);
  }
}

add_item_view_test.dart

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'my_app_mobile/models/graded_item/graded_item.dart';
import 'my_app_mobile/views/dashboard/children/collection/children/add_item/add_item.dart';
import 'package:mockito/mockito.dart';

void main() {
  Widget mountApp({GradedItem gradedItem,void Function() onPhoto}) {
    return MaterialApp(
      home: AddItemView(
        gradedItem: gradedItem,onPhoto: onPhoto,);
  }

  testWidgets('should build with no problems',(tester) async {
    await tester.pumpWidget(mountApp(gradedItem: GradedItem.generate()));
    expect(find.byType(AddItemView),findsOneWidget);
  });

  group('photo',() {
    testWidgets(
      'should photo widget be available',(tester) async {
        await tester.pumpWidget(mountApp(
          gradedItem: GradedItem.generate(),));
        expect(find.byKey(AddItemView.photoKey),findsOneWidget);
      },);
    testWidgets(
      'should be able to call onPhoto handler when is available',(tester) async {
        final fn = MockedFunctions();

        await tester.pumpWidget(mountApp(
          gradedItem: GradedItem.generate(),onPhoto: fn.onPhoto,findsOneWidget);

        await tester.tap(find.byKey(AddItemView.photoKey));
        verify(fn.onPhoto()).called(1);
      },);
    testWidgets(
      'should onPhoto handler open camera options',(tester) async {
        await tester.pumpWidget(
          MaterialApp(
            home: Material(
              child: Builder(
                builder: (BuildContext context) {
                  return AddItemView(
                    gradedItem: GradedItem.generate(),// onPhoto: fn.onPhoto,);

        await tester.tap(find.byKey(AddItemView.photoKey));
        await tester.pumpAndSettle(const Duration(seconds: 1));
        expect(find.byIcon(Icons.camera),);
  });
}

class MockedFunctions extends Mock {
  void onPhoto();
}

有没有办法对 showDialog 函数进行测试?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)