widget_tester_test.dart 1.79 KB
Newer Older
1 2 3 4 5 6 7 8
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
9
  group('findsOneWidget', () {
10 11
    testWidgets('finds exactly one widget', (WidgetTester tester) async {
      await tester.pumpWidget(new Text('foo'));
12
      expect(find.text('foo'), findsOneWidget);
13 14
    });

15
    testWidgets('fails with a descriptive message', (WidgetTester tester) async {
16 17 18 19 20 21 22 23 24 25 26 27
      TestFailure failure;
      try {
        expect(find.text('foo'), findsOneWidget);
      } catch(e) {
        failure = e;
      }

      expect(failure, isNotNull);
      String message = failure.message;
      expect(message, contains('Expected: exactly one matching node in the widget tree\n'));
      expect(message, contains('Actual: ?:<zero widgets with text "foo">\n'));
      expect(message, contains('Which: means none were found but one was expected\n'));
28 29 30
    });
  });

31
  group('findsNothing', () {
32
    testWidgets('finds no widgets', (WidgetTester tester) async {
33
      expect(find.text('foo'), findsNothing);
34 35
    });

36 37
    testWidgets('fails with a descriptive message', (WidgetTester tester) async {
      await tester.pumpWidget(new Text('foo'));
38 39 40 41 42 43 44 45 46 47 48 49 50 51

      TestFailure failure;
      try {
        expect(find.text('foo'), findsNothing);
      } catch(e) {
        failure = e;
      }

      expect(failure, isNotNull);
      String message = failure.message;

      expect(message, contains('Expected: no matching nodes in the widget tree\n'));
      expect(message, contains('Actual: ?:<exactly one widget with text "foo": Text("foo")>\n'));
      expect(message, contains('Which: means one was found but none were expected\n'));
52 53
    });
  });
54

55
}