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.
- make the
DataRepository
object accessible for eachblocTest
functions - 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?