简体   繁体   English

如何为flutter包编写测试 Flutter WeChat Assets Picker

[英]How to write test for flutter package Flutter WeChat Assets Picker

I am trying to write a test on this flutter package https://pub.dev/packages/wechat_assets_picker using the Mocktail package https://pub.dev/packages/mocktail , but this package does not seem to have test in the documentation.我正在尝试使用 Mocktail 包https://pub.dev/packages/mocktail对此颤振包https://pub.dev/packages/wechat_assets_picker编写测试,但该包似乎没有在文档中进行测试.

I have included the minimum reproducible example.我已经包含了最小的可重现示例。 The test file is currently not working, it is included as an example test code using the flutter Mocktail package.测试文件当前不工作,它包含在使用flutter Mocktail 包的示例测试代码中。

It is supposed to mock AssetPicker.pickAssets to test whether it is actually called with the correct arguments.它应该模拟 AssetPicker.pickAssets 以测试它是否实际上是用正确的参数调用的。 I am running in IOS simulator, we will need to add this key in ios/Runer/Info.plist , otherwise the simulator will close unexpectedly after clicking the add button.我是在IOS模拟器上运行的,我们需要在ios/Runer/Info.plist添加这个key,否则点击添加按钮后模拟器会意外关闭。

main.dart主要.dart

import 'package:flutter/material.dart';
import 'package:wechat_assets_picker/wechat_assets_picker.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

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

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: const [],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () async {
          final List<AssetEntity>? result =
              await AssetPicker.pickAssets(context);
        },
        tooltip: 'Add photo',
        child: const Icon(Icons.add),
      ),
    );
  }
}

widget_test.dart widget_test.dart

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart';
import 'package:wechat/main.dart';
import 'package:wechat_assets_picker/wechat_assets_picker.dart';

class _MockAssetPicker extends Mock implements AssetPicker {}

void main() {
  testWidgets('It should call the WeChat asset picker',
      (WidgetTester tester) async {
    await tester.pumpWidget(const MyApp());
    final BuildContext context = tester.element(find.byType(MyApp));

    final assetPickerMock = _MockAssetPicker();
    when(() => assetPickerMock.pickAssets(context)).thenAnswer((_) => Future.value([
          const AssetEntity(
            id: 'id1',
            typeInt: 1,
            width: 100,
            height: 100,
          ),
        ]));
    AssetPicker.instance = assetPickerMock;

    await tester.tap(find.byIcon(Icons.add));
    await tester.pump();

    verify(() => assetPickerMock.pickAssets(context)).called(1);
  });
}

Mocking the picker is supported by the separated picker delegate: https://github.com/fluttercandies/flutter_wechat_assets_picker/pull/315分离的选择器代表支持模拟选择器: https ://github.com/fluttercandies/flutter_wechat_assets_picker/pull/315

TL;DR, build your own delegate first, then set it through AssetPicker.setPickerDelegate(TestAssetPickerDelegate()); TL;DR,先建立自己的委托,然后通过AssetPicker.setPickerDelegate(TestAssetPickerDelegate());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM