How to properly override the initial state of a NotifierProvider for testing. #3232
Unanswered
HugoCSWork
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi,
As mentioned in the title, I am struggling to figure out the best way to override the initial state for a auto generated NotifierProvider for testing.
For context, prior to NotifierProvider we were using StateNotifierProvider and would override the state using -
StateNotifier(this._ref, {State? state}) : super(state ?? State.initial());
and then in a test it's easier to override -
Using auto generated NotifierProvider there doesn't seem an equivalent of this as you cannot override it in the super and using the constructor throws an error
The other approach I've tried which works but throws a linting error with riverpod_lint and I don't like the fact that it's creating a public variable -
The current way I've got it working which I'm still not fully happy with is creating a function to update the state. I apply the visibleForTesting to prevent it from passing any pipelines if I was to misuse it.
Although the solution works it doesn't feel as clean as the initial way of doing it through the StateNotifierProvider. Any input / opinions of alternative solutions would be great!
Beta Was this translation helpful? Give feedback.
All reactions