During WidgetTest, the value that should have been updated is not updated, which is troubling me.
Update the value of _num using the add method in the TrialLogic class below and
class TrialLogic with ChangeNotifier {
late int_num;
int get num {
return_num;
}
void add(a,b){
_num=a+b;
notifyListeners();
}
}
I'd like to test that the updated value is displayed in the TrialWidget class below.
class TrialWidget extensions StatelessWidget{
constTrialWidget({Key?) key}):super(key:key);
@override
Widget build (BuildContext context) {
finalTrialLogic=Provider.of<TrialLogic>(context);
Future()=>trialLogic.add(5,10));
return Text (trialLogic.num.toString());
}
}
After writing and running the test as follows,
testWidgets('Verify updated value is displayed', (widgetTester) async {
waitWidgetTester.pumpWidget(
ChangeNotifierProvider(
create:(_)=>TrialLogic(),
child —MaterialApp(
home —TrialWidget(),
),
),
);
wait widegetTester.pump();
expect(find.text('15'), findsOneWidget);
});
The following error occurred:The value does not appear to have been updated.
_InheritedProviderScope<TrialLogic?>):
LateInitializationError: Field'_num @22387026' has not been initialized.
However, when I checked with the simulator, the updated value was displayed without any problems.
The test runs without waiting for Future-enclosed processing, so
I heard that wait pumpEventQueue() must be inserted before expect to wait for future-enclosed processing, so
I actually tried it, but this time the test is not over at all finish at all.
I didn't know how to wait for the processing surrounded by Future, so please let me know.
Also, if there are any other problems, please let me know.
There seems to be a problem with the design of TrialLogic and TrialWidget.
TrialLogic specifies _num at a rate, and the decision is made with add.
On the other hand, we are adding TrialLogic in Future within TrialWidget, so the get in the next line has not yet confirmed _num.Therefore, it is only natural that the error is presented.
TrialLogic sets the initial value instead of late.
Instead of calling the add method in TrialWidget, this is usually the case where the add is called elsewhere and the result is displayed.
The implementation that works properly is as follows:
class TrialLogic with ChangeNotifier {
int_num = 0;
int get num {
return_num;
}
void add(a,b){
_num=a+b;
notifyListeners();
}
}
classTrialWidget extensionsStatelessWidget{
constTrialWidget({Key?) key}):super(key:key);
@override
Widget build (BuildContext context) {
finalTrialLogic=Provider.of<TrialLogic>(context);
return Text (trialLogic.num.toString());
}
}
Test Part
testWidgets('Verify updated value is displayed', (widgetTester) async {
final data=TrialLogic();
final widget = ChangeNotifierProvider(
create:(_)=>data,
child —const MaterialApp(
home —TrialWidget(),
),
);
waitWidgetTester.pumpWidget(widget);
data.add(5,10);
wait widegetTester.pump();
expect(find.text('15'), findsOneWidget);
});
If you want to view the results of asynchronous data determination, you need to use FutureBuilder to manage the undecided state and display widgets.
Also, the simulator? ran the add method before the TrialWidget build call, so _num was confirmed, so it might be displayed correctly.
If so, the test should be based on simulator processing procedures.
© 2024 OneMinuteCode. All rights reserved.