Está tudo igual conforme é passado no curso
void main() {
testWidgets('Salvar Contato', (tester) async {
final mockContatoDao = MockContatoDao();
await tester.pumpWidget(Banco(
contatoDao: mockContatoDao,
));
final dashboard = find.byType(Dashboard);
expect(dashboard, findsOneWidget);
final transferenciaFeatureItem = find.byWidgetPredicate((widget) =>
fetureItemMatcher(widget, 'Transferencia', Icons.monetization_on));
expect(transferenciaFeatureItem, findsOneWidget);
await tester.tap(transferenciaFeatureItem);
await tester.pumpAndSettle();
final listaContato = find.byType(ListaContato);
expect(listaContato, findsOneWidget);
final fabNovoContato = find.widgetWithIcon(FloatingActionButton, Icons.add);
expect(fabNovoContato, findsOneWidget);
await tester.tap(fabNovoContato);
await tester.pumpAndSettle();
final contatoFormulario = find.byType(FormularioContato);
expect(contatoFormulario, findsOneWidget);
final nomeTextField = find.byWidgetPredicate((widget) {
if (widget is TextField) {
return widget.decoration.labelText == 'Nome Completo';
}
return false;
});
expect(nomeTextField, findsOneWidget);
await tester.enterText(nomeTextField, 'Heron');
final numeroContaTextField = find.byWidgetPredicate((widget) {
if (widget is TextField) {
return widget.decoration.labelText == 'Número da Conta';
}
return false;
});
expect(numeroContaTextField, findsOneWidget);
await tester.enterText(nomeTextField, '1000');
final createButton = find.widgetWithText(RaisedButton, 'Novo');
expect(createButton, findsOneWidget);
await tester.tap(createButton);
await tester.pumpAndSettle();
final listaContatoBack = find.byType(ListaContato);
expect(listaContatoBack, findsOneWidget);
});
}
Mas retorna o erro
EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK The following TestFailure object was thrown running a test: Expected: exactly one matching node in the widget tree Actual: _WidgetTypeFinder:<zero widgets with type "ListaContato" (ignoring offstage widgets)> Which: means none were found but one was expected
When the exception was thrown, this was the stack:
#4 main. (file:///F:/Heron/Flutter/APP-Flutter-Curso-Alura/Curso%202/banco/test/save_contato_test.dart:62:5)
#5 main. (file:///F:/Heron/Flutter/APP-Flutter-Curso-Alura/Curso%202/banco/test/save_contato_test.dart)
#6 testWidgets.. (package:flutter_test/src/widget_tester.dart:140:29)
#7 testWidgets.. (package:flutter_test/src/widget_tester.dart)
#8 TestWidgetsFlutterBinding.runTestBody (package:fluttertest/src/binding.dart:706:19)
#11 TestWidgetsFlutterBinding.runTest (package:fluttertest/src/binding.dart:686:14)
#12 AutomatedTestWidgetsFlutterBinding.runTest. (package:flutter_test/src/binding.dart:1086:24)
#18 AutomatedTestWidgetsFlutterBinding.runTest (package:flutter_test/src/binding.dart:1083:15)
#19 testWidgets. (package:flutter_test/src/widget_tester.dart:133:24)
#20 Declarer.test... (package:test_api/src/backend/declarer.dart:172:27)
#21 Declarer.test... (package:test_api/src/backend/declarer.dart)
#22 Invoker.waitForOutstandingCallbacks. (package:test_api/src/backend/invoker.dart:246:15)
#27 Invoker.waitForOutstandingCallbacks (package:test_api/src/backend/invoker.dart:243:5)
#28 Declarer.test.. (package:test_api/src/backend/declarer.dart:170:33)
#33 Declarer.test. (package:test_api/src/backend/declarer.dart:169:13)
#34 Invoker.onRun.... (package:testapi/src/backend/invoker.dart:400:25) (elided 31 frames from class FakeAsync, class _RawReceivePortImpl, class _Timer, dart:async, dart:async-patch, and package:stacktrace)
This was caught by the test expectation on the following line: file:///F:/Heron/Flutter/APP-Flutter-Curso-Alura/Curso%202/banco/test/save_contato_test.dart line 62 The test description was: Salvar Contato
Test failed. See exception logs above. The test description was: Salvar Contato
mas se passar o final listaContatoBack = find.byType(formContato ); ele retorna correto, sendo assim ele não consegue sair da tela de Formulario no teste