You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'd like to know about testing my widget with Obx().
I'm trying to create two scenarios in the same test. I know I could split it into two tests, but just for curiousity, I'd like to know if it's possible to test using Obx.
Follow the steps:
Gostaria de saber como testar meu widget utilizando Obx(). Estou tentando criar dois cenários no mesmo teste. Eu sei que eu deveria separá-los, mas apenas por curiosidade, eu gostaria de saber se é possível criar um test com Obx. Estou seguindo os seguintes passos:
Inicializo o widget -> Faço modificações no Controller -> Atualizo o widget
testWidgets('OrderItem list is not empty', (WidgetTester tester) async {
await tester.pumpWidget(app);
final keyNoItemWidgetFinder = find.byKey(constKey(HomeView.keyNoItemWidget));
final keyListOrderFinder = find.byKey(constKey(HomeView.keyListOrder));
expect(homeView.controller.order.items.isEmpty, true);
expect(keyNoItemWidgetFinder, findsOneWidget);
expect(keyListOrderFinder, findsNothing);
homeView.controller.order.addOrderItem(
OrderItemModel(product:ProductModel(id:1, description:'Product 1', price:1)),
);
homeView.controller.order.addOrderItem(
OrderItemModel(product:ProductModel(id:2, description:'Product 2', price:2)),
);
await tester.pump();
expect(homeView.controller.order.items.isEmpty, false);
expect(keyNoItemWidgetFinder, findsNothing);
expect(keyListOrderFinder, findsOneWidget);
});
Erro na seguinte linha (means one was found but none were expected)
expect(keyNoItemWidgetFinder, findsNothing);
If I try to test just one scenario, it works fine: Se eu separar e mantê-lo da seguinte maneira, funciona:
testWidgets('OrderItem list is not empty', (WidgetTester tester) async {
homeView.controller.order.addOrderItem(
OrderItemModel(product:ProductModel(id:1, description:'Product 1', price:1)),
);
homeView.controller.order.addOrderItem(
OrderItemModel(product:ProductModel(id:2, description:'Product 2', price:2)),
);
await tester.pumpWidget(app);
final keyNoItemWidgetFinder = find.byKey(constKey(HomeView.keyNoItemWidget));
final keyListOrderFinder = find.byKey(constKey(HomeView.keyListOrder));
expect(homeView.controller.order.items.isEmpty, false);
expect(keyNoItemWidgetFinder, findsNothing);
expect(keyListOrderFinder, findsOneWidget);
});
I'd like to test using pump() and Obx(). Mas gostaria de utilizar o pump() com o Obx().
[SOLUTION]:
We shouldn't manipulate the controller directly, instead of it, use the widgets available on the screen.
I mean, I was trying to add items to the list by calling the controller's method. So, instead of if, there is a button on the widget to manipulate the list. I just tap on it.
Nós não devemos manipular o controller diretamente, e ao invés disso, devemos utilizar os widgets disponíveis na tela.
Em outras palavras, eu estava adicionando itens na lista através de um método disponível no controller. Então, ao invés de seguir essa abordagem, existe um botão no Widget para manipular a lista. Em resumo, estou apenas pressionando o botão.
testWidgets('OrderItem list is not empty', (WidgetTester tester) async {
await tester.pumpWidget(app);
final keyNoItemWidgetFinder = find.byKey(constKey(HomeView.keyNoItemWidget));
final keyListOrderFinder = find.byKey(constKey(HomeView.keyListOrder));
final keyButtonAddOrderItem = find.byKey(constKey(HomeView.keyButtonAddOrderItem));
expect(homeView.controller.order.items.isEmpty, true);
expect(keyNoItemWidgetFinder, findsOneWidget);
expect(keyListOrderFinder, findsNothing);
await tester.tap(keyButtonAddOrderItem);
await tester.pump();
expect(homeView.controller.order.items.isEmpty, false);
expect(keyNoItemWidgetFinder, findsNothing);
expect(keyListOrderFinder, findsOneWidget);
});
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I'd like to know about testing my widget with Obx().
I'm trying to create two scenarios in the same test. I know I could split it into two tests, but just for curiousity, I'd like to know if it's possible to test using Obx.
Follow the steps:
Gostaria de saber como testar meu widget utilizando Obx().
Estou tentando criar dois cenários no mesmo teste. Eu sei que eu deveria separá-los, mas apenas por curiosidade, eu gostaria de saber se é possível criar um test com Obx.
Estou seguindo os seguintes passos:
expect(keyNoItemWidgetFinder, findsNothing);
If I try to test just one scenario, it works fine:
Se eu separar e mantê-lo da seguinte maneira, funciona:
I'd like to test using pump() and Obx().
Mas gostaria de utilizar o pump() com o Obx().
[SOLUTION]:
We shouldn't manipulate the controller directly, instead of it, use the widgets available on the screen.
I mean, I was trying to add items to the list by calling the controller's method. So, instead of if, there is a button on the widget to manipulate the list. I just tap on it.
Nós não devemos manipular o controller diretamente, e ao invés disso, devemos utilizar os widgets disponíveis na tela.
Em outras palavras, eu estava adicionando itens na lista através de um método disponível no controller. Então, ao invés de seguir essa abordagem, existe um botão no Widget para manipular a lista. Em resumo, estou apenas pressionando o botão.
Beta Was this translation helpful? Give feedback.
All reactions