Estou com um erro no teste quando ele cria um contato e deve retornar para a Lista de Contatos. Ele exibe este erro:
The following TestFailure object was thrown running a test:
Expected: exactly one matching node in the widget tree
Actual: _WidgetTypeFinder:<zero widgets with type "ContactsList" (ignoring offstage widgets)>
Which: means none were found but one was expected
Ja converti a ContactsList para StatefulWidget e não corrigiu o problema.
Aqui meu arquivo de teste:
@GenerateMocks([ContactDao])
void main() {
testWidgets('Should save a contact', (tester) async {
final mockContactDao = MockContactDao();
when(mockContactDao.findAll()).thenAnswer((_) async => []);
when(mockContactDao.save(any)).thenAnswer((_) async => 1);
await tester.pumpWidget(BytebankApp(
contactDao: mockContactDao,
));
[...]
final contactForm = find.byType(ContactForm);
expect(contactForm, findsOneWidget);
final nameTextField = find.byWidgetPredicate((widget) {
if (widget is TextField) {
return widget.decoration!.labelText == 'Full name';
}
return false;
});
expect(nameTextField, findsOneWidget);
await tester.enterText(nameTextField, 'Alex');
final accountNumberTextField = find.byWidgetPredicate((widget) {
if (widget is TextField) {
return widget.decoration!.labelText == 'Account number';
}
return false;
});
expect(accountNumberTextField, findsOneWidget);
await tester.enterText(accountNumberTextField, '1000');
final createButton = find.widgetWithText(ElevatedButton, 'Create');
expect(createButton, findsOneWidget);
await tester.tap(createButton);
await tester.pumpAndSettle();
final contactsListBack = find.byType(ContactsList);
expect(contactsListBack, findsOneWidget);
});
}
ContactsList
class ContactsList extends StatefulWidget {
final ContactDao contactDao;
ContactsList({required this.contactDao});
@override
_ContactsListState createState() =>
_ContactsListState(contactDao: contactDao);
}
class _ContactsListState extends State<ContactsList> {
final ContactDao contactDao;
_ContactsListState({required this.contactDao});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Transfer'),
),
body: FutureBuilder<List<Contact>>(
initialData: [],
future: widget.contactDao.findAll(),
builder: (context, snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.none:
break;
case ConnectionState.waiting:
return Progress();
break;
case ConnectionState.active:
break;
case ConnectionState.done:
final List<Contact>? contacts = snapshot.data;
return ListView.builder(
itemBuilder: (context, index) {
final Contact contact = contacts![index];
return _ContactItem(
contact,
onClick: () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => TransactionForm(contact),
),
);
},
);
},
itemCount: contacts!.length,
);
break;
}
return Text('Unknown error');
},
),
floatingActionButton: FloatingActionButton(
onPressed: () {
Navigator.of(context)
.push(MaterialPageRoute(
builder: (context) => ContactForm(contactDao: contactDao)))
.then((newContact) {
if (newContact != null) {
setState(() {
contactDao.findAll();
});
}
});
},
child: Icon(
Icons.add,
),
),
);
}
}
class _ContactItem extends StatelessWidget {
final Contact contact;
final Function onClick;
_ContactItem(
this.contact, {
required this.onClick,
});
@override
Widget build(BuildContext context) {
return Card(
child: ListTile(
onTap: () => onClick(),
title: Text(
contact.name,
style: TextStyle(
fontSize: 24.0,
),
),
subtitle: Text(
contact.accountNumber.toString(),
style: TextStyle(
fontSize: 16.0,
),
),
),
);
}
}