testing riverpod statenotifierprovider
an example of how to mock state for testing widgets when using riverpod statenotifierprovider.
these tests work without mocktail / mockito
quick details
- model: counter class created with freezed
- state: counternotifier extends statenotifier
- provider: counterprovider is a statenotifierprovider
important for testability of statenotifierprovider
when testing flutter widgets, one may want to mock a predefined state in order to test how widgets render for a given state. for example, a widget may need to show or hide depending on user role, which could be part of a user object stored in state. our test allows us to ensure this important conditional rendering does not break with future code changes.
in this simplified example, we are using a counter to test whether a widget renders when the count is even.
this repository exists as an example for how to architect your statenotifier
and its constructor such that a mocked provider and predefined state can be used by the test widget.
below is a breakdown of what is happening in this repository.
explanation
the test widget
widget iseventestwidget(statenotifierprovider<counternotifier, counter> mockprovider) {
return providerscope(
overrides: [
counterprovider.overridewithprovider(mockprovider),
],
child: const materialapp(
home: screenhome(),
),
);
}
this test widget for our home screen uses the overrides
property of providerscope()
in order to override the provider used in the widget.
when the home.dart screenhome()
widget calls counter counter = ref.watch(counterprovider);
it will use our mockprovider
instead of the “real” provider.
the iseventestwidget()
mockprovider argument is the same “type” of provider as counterprovider()
.
the test
testwidgets('if count is even, isevenmessage is rendered.', (tester) async {
// mock a provider with an even count
final mockcounterprovider =
statenotifierprovider<counternotifier, counter>((ref) => counternotifier(counter: const counter(count: 2)));
await tester.pumpwidget(iseventestwidget(mockcounterprovider));
expect(find.bytype(isevenmessage), findsonewidget);
});
in the test, we create a mockprovider with predefined values that we need for testing screenhome()
widget rendering. in this example, our provider is initialized with the state count: 2
.
we are testing that the isevenmessage()
widget is rendered with an even count (of 2). another test tests that the widget is not rendered with an odd count.
statenotifier constructor
class counternotifier extends statenotifier<counter> {
counternotifier({counter counter = const counter(count: 0)}) : super(counter);
void increment() {
state = state.copywith(count: state.count + 1);
}
}
in order to be able to create a mockprovider with a predefined state, it is important that the statenotifier (counter_state.dart
) constructor includes an optional parameter of the state model. the default argument is how the state should normally initialize. our tests can optionally provide a specified state for testing which is passed to super()
.
build runner
code generator is required for freezed class code generation
$ flutter pub run build_runner build --delete-conflicting-outputs
Comments are closed.