-
Set up the project by adding bloc_test and flutter_bloc packages in pubspec.yaml. Run the command to install them.
-
Create the CounterCubit, which is a class that extends Cubit and controls a counter with increment and decrement methods. The initial state is zero.
-
In the test directory, create a file to write unit tests for CounterCubit.
-
Declare a CounterCubit variable using the
late
keyword, allowing initialization later in the setup for each test. -
Use setup and teardown functions to initialize and close CounterCubit before and after each test. In setup, create a new instance of CounterCubit, and in teardown, close the cubit to release resources.
-
Group all tests related to CounterCubit together for readability.
-
Write a test to check if the initial state of CounterCubit is zero, using
expect
to verify that the state is zero at the start. -
Use blocTest for increment and decrement. Each blocTest has several parts:
- Specify the cubit and state type (CounterCubit with an integer state)
- Build: supply the instance of CounterCubit created in setup
- Act: define the action to perform on CounterCubit, like calling increment
- Expect: define the expected output state after the action. For example, if calling increment, the expected state would be one; if calling decrement, the expected state would be negative one.
-
Build creates the CounterCubit instance, act performs the action, and expect checks if the resulting state matches the expected value after the action.
-
Notifications
You must be signed in to change notification settings - Fork 0
The repo demonstrates unit testing a CounterCubit class using bloc_test and flutter_bloc. It includes tests for the initial state and state changes (increment/decrement), with setup and teardown functions for proper cubit initialization and resource management.
axah710/counter_cubit_unit_testing
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
About
The repo demonstrates unit testing a CounterCubit class using bloc_test and flutter_bloc. It includes tests for the initial state and state changes (increment/decrement), with setup and teardown functions for proper cubit initialization and resource management.
Resources
Stars
Watchers
Forks
Releases
No releases published
Packages 0
No packages published