In a BlocTest, you can test the bloc, state, and event simultaneously since they are interdependent. To test the bloc in isolation, the test requires the creation of mocks, which can be generated with the @GenerateMocks annotation. You'll prepare your test like this:
However, note that @GenerateMocks only generates the mock classes during compile time, and therefore you need to execute a command in the terminal to generate the mocks.dart file. To generate the mocks.dart file, use the following command:
Before the actual test begins, a variable called playerRepo is declared without initialization. The setUpAll method is then called to perform any necessary setup for the test to pass. In this case, a MockPlayerRepositoryTest instance is created and assigned to playerRepo.
The `group('Create Player Tests: ', () {` section of the test is used to group together tests with similar functionality. The string serves as a description of the test group.
Then, a BasicResponse class is created to represent what a good response should look like. The CreatePlayerBloc and CreatePlayerState are then passed into blocTest.
The test is given the description "Check flow of states" to indicate what it is testing for. Inside the build method, when the createPlayer method is called with any argument passed to createPlayer, the mock method should return the good response, which is a BasicResponse with success = true and description = "created".
In the act method, a SendCreatePlayerEvent with "fred", "pw", 1, 2, 3 being passed to it as parameters. The event is then added to the bloc, which triggers the bloc's event listener. The test is instructed to wait for 500 milliseconds to allow the bloc to complete state transitions.
Lastly, the test asserts whether the bloc has successfully emitted the CreatePlayerLoading and CreatePlayerComplete states.
---
Generally speaking, the main part that will change between tests is what happens in the build method. That will depend on what is being tested.