0

I am here for clarification about unit tests in Flutter.

Here is a typical test snippet that I use to test my flutter_bloc bloc component.

void main() {
  TestWidgetsFlutterBinding.ensureInitialized();
  ///we have only one data repository which is referred in all blocTests defined
  ///in this file
  DataRepository dataRepository;
  setUp(() {
    dataRepository = mockData.MockDataRepository();
  });

  blocTest('User can login with a valid username and valid password',
      build: () {
       ///code referring dataRepository
        when(dataRepository.login("username","password"))
            .thenAnswer((_) async =>
            mockData.setupSuccessObject(mockData.setupUser()));

        return LoginPageBloc(
          dataRepository: dataRepository,);
      },
      act: (bloc){
    ///...
      },
      expect: [
        ///...
      ],
      verify: () async {
        ///code referring dataRepository
        verify(dataRepository.login("username", "password"))
            .called(1);
      });

  blocTest('User cannot login with invalid username or password',
      build: () {
        ///code referring dataRepository
        when(dataRepository.login("username","password"))
            .thenAnswer((_) async =>
            mockData.setupAuthFailedObject());

        return LoginPageBloc(
            dataRepository: dataRepository,);
      },
      act: (bloc){
        ///...
      },
      expect: [
        ///...
      ],
      verify: () async {
        ///code referring dataRepository
        verify(dataRepository.login("username", "password"))
            .called(1);
      });

}

In the above snippet, you can see that the DataRepostory object made declared as a top-level variable in order to make the following possible.

  1. make the DataRepository object accessible for each blocTest functions
  2. make the DataRepository object accessible for each 'build' and 'verify' methods within the blocTest() method.

My question is,

is there any side effect for using a global top-level variable like this when we are running tests in parallel?

Rémi Rousselet
  • 256,336
  • 79
  • 519
  • 432
Code Wizard
  • 319
  • 3
  • 8
  • Why don't you just create the repository inside the `build` function of `blocTest`? – Rémi Rousselet Feb 17 '20 at 12:25
  • if I create the repository object inside the build function, then how can I refer that repository object inside 'verify' method to verify whether or not the login method get called? – Code Wizard Feb 17 '20 at 12:49
  • Your bloc should have a reference to the repository – Rémi Rousselet Feb 17 '20 at 12:56
  • yes, bloc has a reference to the repository. But, where should I I keep the reference to the bloc? seems like the bloc object also not available on the 'verify' method. If I keep the bloc instance instead of the datarepository as the top level variable, am I in the same problem of having side effects?? – Code Wizard Feb 17 '20 at 12:57
  • @RémiRousselet sir, the above tests running green, but I am worrying about the side effects? since the bloc is not available in the 'verify' method, what should I do sir? – Code Wizard Feb 17 '20 at 13:09

0 Answers0