Solucionado (ver solução)
Solucionado
(ver solução)
1
resposta

Refatoração de teste

Fiz as modificações conforme a orientação da atividade 07 Refatoração de teste. dá o seguinte erro:

Testing started at 08:15 ...
EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK 
The following StateError was thrown running a test:
Bad state: No element

When the exception was thrown, this was the stack:
#0      Iterable.single (dart:core/iterable.dart:558:25)
#1      WidgetController.state (package:flutter_test/src/controller.dart:155:42)
#2      WidgetTester.showKeyboard.<anonymous closure> (package:flutter_test/src/widget_tester.dart:1066:42)
#5      TestAsyncUtils.guard (package:flutter_test/src/test_async_utils.dart:72:41)
#6      WidgetTester.showKeyboard (package:flutter_test/src/widget_tester.dart:1065:27)
#7      WidgetTester.enterText.<anonymous closure> (package:flutter_test/src/widget_tester.dart:1089:13)
#10     TestAsyncUtils.guard (package:flutter_test/src/test_async_utils.dart:72:41)
#11     WidgetTester.enterText 
...

meu código sem os imports transfer_flow.dart

void main() {
  MockContactDao mockContactDao;
  MockTransactionWebClient mockTransactionWebClient;

  setUp(() async {
    mockContactDao = MockContactDao();
    mockTransactionWebClient = MockTransactionWebClient();
  });

  testWidgets('Should transfer to a contact', (tester) async {
    await tester.pumpWidget(BytebankApp(
      transactionWebClient: mockTransactionWebClient,
      contactDao: mockContactDao,
    ));

    final dashboard = find.byType(Dashboard);
    expect(dashboard, findsOneWidget);
    var alex = Contact(0, 'Alex', 1000);
    when(mockContactDao.findAll()).thenAnswer((invocation) async => [alex]);

    await clickOnTheTransferFeatureItem(tester);
    await tester.pumpAndSettle();

    final contactsList = find.byType(ContactsList);
    expect(contactsList, findsOneWidget);

    verify(mockContactDao.findAll()).called(1);

    final contactItem = find.byWidgetPredicate((widget) {
      if (widget is ContactItem) {
        return widget.contact.name == 'Alex' &&
            widget.contact.accountNumber == 1000;
      }
      return false;
    });
    expect(contactItem, findsOneWidget);
    await tester.tap(contactItem);
    await tester.pumpAndSettle();

    final transactionForm = find.byType(TransactionForm);
    expect(transactionForm, findsOneWidget);

    await fillTextFieldWithLabel(tester, 'Alex');
    await tester.pumpAndSettle();

    await fillTextFieldWithLabel(tester, '1000');
    await tester.pumpAndSettle();

    final textFieldValue = find.byWidgetPredicate((widget) {
      return textFieldByLabelTextMatcher(widget, 'Value');
    });
    expect(textFieldValue, findsOneWidget);

    await tester.enterText(textFieldValue, '200');

    await clickOntheRaisedButtonWithText(tester, 'Transfer');
    await tester.pumpAndSettle();

    final transactionAuthDialog = find.byType(TransactionAuthDialog);
    expect(transactionAuthDialog, findsOneWidget);

    final textFieldPassword =
        find.byKey(transactionAuthDialogTextFieldPasswordKey);
    expect(textFieldPassword, findsOneWidget);
    await tester.enterText(textFieldPassword, '1000');

    await clickOntheFlatButtonWithText(tester, 'Cancel');
    await tester.pumpAndSettle();

    await clickOntheFlatButtonWithText(tester, 'Confirm');
    await tester.pumpAndSettle();

    when(mockTransactionWebClient.save(Transaction(null, 200, alex), '1000'))
        .thenAnswer((_) async => Transaction(null, 200, alex));

    await tester.pumpAndSettle();

    final successDialog = find.byType(SuccessDialog);
    expect(successDialog, findsOneWidget);

    await clickOntheFlatButtonWithText(tester, 'Ok');
    await tester.pumpAndSettle();

    final contactsListBack = find.byType(ContactsList);
    expect(contactsListBack, findsOneWidget);

    // verify(mockContactDao.findAll());
  });
}
Future<void> clickOntheFlatButtonWithText(WidgetTester tester, String text) async {
  final botao = find.widgetWithText(FlatButton, text);
  expect(botao, findsOneWidget);
  return tester.tap(botao);
}

Future<void> clickOntheRaisedButtonWithText(WidgetTester tester, String text) async {
  final botao = find.widgetWithText(RaisedButton, text);
  expect(botao, findsOneWidget);
  return tester.tap(botao);
}

Future<void> fillTextFieldWithLabel(WidgetTester tester, String labelText) async {
  final contactName = find.text(labelText);
  expect(contactName, findsOneWidget);
  return tester.enterText(contactName, labelText);
}

Podem me ajudar?

1 resposta
solução!
import 'package:bytebank/components/response_dialog.dart';
import 'package:bytebank/components/transaction_auth_dialog.dart';
import 'package:bytebank/main.dart';
import 'package:bytebank/models/contact.dart';
import 'package:bytebank/models/transaction.dart';
import 'package:bytebank/screens/contacts_list.dart';
import 'package:bytebank/screens/dashboard.dart';
import 'package:bytebank/screens/transaction_form.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/mockito.dart';

import '../matchers/matchers.dart';
import '../mocks/mocks.dart';
import 'actions.dart';

void main() {
  MockContactDao mockContactDao;
  MockTransactionWebClient mockTransactionWebClient;

  setUp(() async {
    mockContactDao = MockContactDao();
    mockTransactionWebClient = MockTransactionWebClient();
  });

  testWidgets('Should transfer to a contact', (tester) async {

    await tester.pumpWidget(BytebankApp(
      transactionWebClient: mockTransactionWebClient,
      contactDao: mockContactDao,
    ));

    final dashboard = find.byType(Dashboard);
    expect(dashboard, findsOneWidget);

    final alex = Contact(0, 'Alex', 1000);
    when(mockContactDao.findAll()).thenAnswer((invocation) async => [alex]);

    await clickOnTheTransferFeatureItem(tester);
    await tester.pumpAndSettle();

    final contactsList = find.byType(ContactsList);
    expect(contactsList, findsOneWidget);

    verify(mockContactDao.findAll()).called(1);

    final contactItem = find.byWidgetPredicate((widget) {
      if (widget is ContactItem) {
        return widget.contact.name == 'Alex' &&
            widget.contact.accountNumber == 1000;
      }
      return false;
    });
    expect(contactItem, findsOneWidget);
    await tester.tap(contactItem);
    await tester.pumpAndSettle();

    final transactionForm = find.byType(TransactionForm);
    expect(transactionForm, findsOneWidget);

    final contactName = find.text('Alex');
    expect(contactName, findsOneWidget);
    final contactAccountNumber = find.text('1000');
    expect(contactAccountNumber, findsOneWidget);

    final textFieldValue = find.byWidgetPredicate((widget){
      return textFieldByLabelTextMatcher(widget, 'Value');
    });
    expect(textFieldValue, findsOneWidget);
    await tester.enterText(textFieldValue, '200');

    await clickOnTheRaisedButtonWithText(tester, 'Transfer');
    await tester.pumpAndSettle();

    final transactionAuthDialog = find.byType(TransactionAuthDialog);
    expect(transactionAuthDialog, findsOneWidget);

    final textFieldPassword = find.byKey(transactionAuthDialogTextFieldPasswordKey);
    expect(textFieldPassword, findsOneWidget);
    await tester.enterText(textFieldPassword, '1000');

    final cancelButton = find.widgetWithText(FlatButton, 'Cancel');
    expect(cancelButton, findsOneWidget);
    final confirmButton = find.widgetWithText(FlatButton, 'Confirm');
    expect(confirmButton, findsOneWidget);

    when(mockTransactionWebClient.save(Transaction(null, 200, alex), '1000'))
        .thenAnswer((_) async => Transaction(null, 200, alex));

    await tester.tap(confirmButton);
    await tester.pumpAndSettle();

    final successDialog = find.byType(SuccessDialog);
    expect(successDialog, findsOneWidget);

    await clickOnTheFlatButtonWithText(tester,'Ok');
    await tester.pumpAndSettle();

    final contactsListBack = find.byType(ContactsList);
    expect(contactsListBack, findsOneWidget);
  });
}

Future<void> clickOnTheFlatButtonWithText(WidgetTester tester, String text) async {
  final okButton = find.widgetWithText(FlatButton, text);
  expect(okButton, findsOneWidget);
  return tester.tap(okButton);
}

Future<void> clickOnTheRaisedButtonWithText(WidgetTester tester, String text) async {
  final transferButton = find.widgetWithText(RaisedButton, text);
  expect(transferButton, findsOneWidget);
  return tester.tap(transferButton);
}