Flutter BLoC or Cubit Test

Flutter BLoC or Cubit Test

    Flutter BLoC or Cubit is one of the most popular for state management. We can use bloc_test package for unit test for Flutter. Using bloc_test package you may get rid of unnecessary boilerplate code.

    Learn about BLoC and TDD here

    Go ahead and install it like below

    flutter pub add -d bloc_test && flutter pub get

    To use it, you will start with 

    blocTest(should:'should', build:'build');

    The test for BLoC or Cubit is the same. For the should section you should mention emit states. You need to mention the emit() functions. 

    See how blocTest() takes Cubit and State class. You may also pass Bloc and State class. The parameters inside <> just like the parameter when you extend Bloc for your class. When you class extend Bloc class you pass bloc class and state class. Here we are doing the same.

    Here inside 'should emit [CreatingUser, UserCreated] when successful' means first create a user and return the created user. Here CreatingUser and UserCreated are states defined AuthenticationState.

    After that for the build parameter when pass an anonymous function. Inside the anonymous function we have to use our usecase. From here on everything else same as a regular TDD test.

    Courses


    Recommended posts


    Recent posts