I am developing an android / ios application in flutter, and I have chosen to use redux for my state management.
I am writing unit tests for my redux actions, which have been implemented using the async_redux package.
I am following the excellent guidelines set out for testing by the author of the package, but I am not sure how to mock the dispatch of further actions from my action under test.
For example, the below LogoutAction
dispatchs a DeleteDatabaseAction
and waits for it to complete:
class LogoutAction extends ReduxAction<AppState> {
@override
Future<AppState> reduce() async {
await dispatchFuture(DeleteDatabaseAction());
return AppState.initialState();
}
}
class DeleteDatabaseAction extends ReduxAction<AppState> {
@override
FutureOr<AppState> reduce() {
throw StateError(
'Unwanted call to runtime implementation of DeleteDatabaseAction',
);
}
}
void main() {
final store = Store<AppState>(initialState: AppState(loggedIn: true));
final storeTester = StoreTester.from(store);
test('Logout action should return correct state and not throw StateError', () async {
storeTester.dispatch(LogoutAction());
TestInfo<AppState> info = await storeTester.wait(LogoutAction);
expect(info.state.loggedIn, false);
});
}
I want to test only the action under test, and stub out all further action calls.
i.e. How can I mock / stub the dispatch
and dispatchFuture
methods on ReduxAction
, so that the runtime DeleteDatabaseAction
implementation is not run?
So far I have attempted:
Inject
DeleteDatabaseAction
using get_it and inject a mock during test- I have 100+ actions that will now need to be added to my context
- Some actions have parameters that change based on where they are called from, so cannot be registered at app startup
Subclass
Store
, override the above methods and use the subclass in my test herefinal store = Store<AppState>(initialState: AppState(loggedIn: true))
- I will not be able to dispatch my action under test, as it uses the same store in the async_redux test implementation
- Here:
storeTester.dispatch(LogoutAction());
- Create a separate
Dispatcher
implementation, inject this and override with a mock during tests- This will work, but it is new framework, I can go this route but now I am deviating from the well documented framework provided by asyn_redux