Olá pessoal, rodando o app ele funciona perfeitamente, porém no teste não parece estar validadando a chamada de mockContactService.readContacts(), o equivalente ao ContactDao.findAll(), ensinado em curso.
segue o Código do teste: Teste do fluxo de adicionar o contato:
import 'package:alura_testes/main.dart';
import 'package:alura_testes/models/contact_model.dart';
import 'package:alura_testes/pages/contact_form_page.dart';
import 'package:alura_testes/pages/contact_list_page.dart';
import 'package:alura_testes/pages/dashboard_page.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/mockito.dart';
import 'matchers.dart';
import 'save_content_flow_test.mocks.dart';
Contact mockContact = Contact(id: '1', name: 'Rene', accountNumber: 0001);
void main() {
testWidgets('Should save a contact', (WidgetTester tester) async {
final mockContactService = MockContactService();
when(mockContactService.readContacts())
.thenAnswer((invocation) async => Future.value([mockContact]));
// Carrega a página de dashboard
await tester.pumpWidget(ByteBankApp(
contactService: mockContactService,
));
final dashboard = find.byType(DashboardPage);
expect(dashboard, findsOneWidget);
final transferFeatureItem = find.byWidgetPredicate(
(widget) =>
dashboarItemMatcher(widget, 'Transfer', Icons.monetization_on),
);
expect(transferFeatureItem, findsOneWidget);
await tester.tap(transferFeatureItem);
//hack por conta do bug com o circularProgress animation
for (int i = 0; i < 5; i++) {
await tester.pump(const Duration(seconds: 1));
}
// Carrega a página de lista de contatos
final contactsList = find.byType(ContactListPage);
expect(contactsList, findsOneWidget);
// verifica se a função readContacts foi chamada
verify(mockContactService.readContacts()).called(1);
...
});
}
Página de Lista de contato:
...
class ContactListPage extends StatefulWidget {
final ContactService contactService;
const ContactListPage({Key? key, required this.contactService})
: super(key: key);
@override
State<ContactListPage> createState() => _ContactListPageState();
}
class _ContactListPageState extends State<ContactListPage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Transfer'),
),
body: FutureBuilder<List<Contact>>(
initialData: const [],
future: widget.contactService.readContacts(),
builder: (ctx, snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.none: //future não executado
break;
case ConnectionState.waiting:
return const Loading();
case ConnectionState
.active: // tem dado disponível mas não terminou a execuçao, como uma Stream
break;
case ConnectionState.done:
final contacts = snapshot.data;
if (contacts == null || contacts.isEmpty) {
return const Center(
child: CenteredMessage(
message: 'Nenhum Contato cadastrado',
icon: Icons.warning,
),
);
}
return ListView.builder(
itemCount: contacts.length,
itemBuilder: (ctx, index) => ContactListItem(
contact: contacts[index],
onTap: () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => TransactionFormPage(
contact: contacts[index],
),
),
);
},
),
);
}
return const CenteredMessage(message: 'Unkonwn error');
},
),
...
}
}