单独测试小部件

时间:2020-12-20 14:05:34

标签: flutter flutter-test widget-test-flutter

我正在学习为我的 Flutter 应用编写 Widget 测试,但在这次测试中迷失了方向。我有一个非常简单的 StatelessWidget,我想检查一下:

  • 关闭 AppBar 的标题
  • 动作图标
  • 正文
import 'package:flutter/material.dart';

class ShowsPage extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: Key("ShowsPage"),
      appBar: AppBar(
        title: Text("Shows"),
        actions: <Widget>[
          IconButton(
              icon: const Icon(Icons.filter),
              onPressed: null)
        ],
      ),
      body: const Center(
        child: Text(
          "This is the Shows page",
        ),
      ),
    );
  }
}

这个 Widget 被 Main 类使用

import 'package:flutter/material.dart';
import 'package:showring_helper/mainPages/showsPage.dart';

void main() {
  runApp(Main());
}

class Main extends StatelessWidget {
  // This widget is the root of your application.

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter app',
      theme: ThemeData(
        primarySwatch: Colors.brown,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: ShowsPage(),
    );
  }
}

我的想法是我可以在没有 Main 类的情况下测试 ShowPage,但我得到这个工作的唯一方法是使用这个测试 tester.pumpWidget(Main())

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

void main() {
  group('Shows Page', ()
  {
    testWidgets('Showspage should have AppBar title Shows', (
        WidgetTester tester) async {
      await tester.pumpWidget(Main());
      var scaffold = find.byType(Scaffold).evaluate().first.widget as Scaffold;
      var appbar = scaffold.appBar as AppBar;
      var title = appbar.title as Text;
      expect(title.data, "Shows");
    });

    testWidgets('ShowPage should have filter icon in AppBar', (
        WidgetTester tester) async {
      await tester.pumpWidget(Main());
      expect(find.byIcon(Icons.filter), findsOneWidget);
    });

    testWidgets('ShowPage should have body child with text', (
        WidgetTester tester) async {
      await tester.pumpWidget(Main());
      var scaffold = find.byType(Scaffold).evaluate().first.widget as Scaffold;
      var body = scaffold.body as Center;
      var child = body.child as Text;
      expect(child.data, "This is the Shows page");
    });
  });
}

有什么方法可以使用 tester.pumpWidget(ShowsPage())

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:showring_helper/mainPages/showsPage.dart';

void main() {
  group('Shows Page', ()
  {
    testWidgets('Showspage should have AppBar title Shows', (
        WidgetTester tester) async {
      await tester.pumpWidget(ShowsPage());
      var scaffold = find.byType(Scaffold).evaluate().first.widget as Scaffold;
      var appbar = scaffold.appBar as AppBar;
      var title = appbar.title as Text;
      expect(title.data, "Shows");
    });

0 个答案:

没有答案