قالب وردپرس درنا توس
Home / IOS Development / Change things up in the tests

Change things up in the tests



Dependency injection is a great tool for breaking up your addiction creation to reusable parts.
This helps separate your code into logical bits where someone makes the addictions and others
consume them. This separation helps in unit tests because you have a clear list of
addiction for each object so you can easily pass in mock versions.

In this post I will investigate the use of addiction injection in integration tests with Espresso.
Some components, such as a network or database layer, must be replaced with fake versions
faster tests that are sealed from the outside. A good addiction injection can give one
Path for you to change the components you enter into the test environment. The pilot project
(connected at the end of this post) uses Dagger 2.1

5 along with the Dagger Android libraries
simplify setup.

Production setup

This entry starts with the main day tag code before going to the test setup. The
The configuration is pretty standard, so not much time will be spent covering how it works in detail.

First, a module is created to provide the dependencies the app needs. In this case, the module is
provides a NerdStore that tracks a list of nerds. The function at the top of the module is
main supplier function. The other bottom is an implementation detail that only provides
addictions the nerd shop needs. Implementing modules in this way provides a fine separation
of worries and ensures that if you want to change the modules in the test, you just need to give one
single dependency per module since none of the other dependencies are used elsewhere in the project. @Module
class AppModule {

@Provides
@Singleton
moro ] provideNerdStore ( nerd : : ] List < Nerd >): NerdStore {
return LiveNerdStore [19659014] ( nerds )
}

] // Implementation details provide features below

@Provides
fun provideNerds (): List < Nerd > [ return listOf return listOf (19659043] Nerd "Brian" ),
Nerd ( "Kristin" ),
Nerd ( "Chris" )
)
}
}

Then, another module is created to specify the classes that need the dependencies. Lean on
@ContributesAndroidInjector annotation here simplifies this file so that separate subcomponents do not
must be created for each activity or added a DispatchingAndroidInjector .

  @Module 
  Abstract    Injector Module    {

      @ContributesAndroidInjector 
      abstract    moron    contributeNerdListActivityInjector   ():    NerdListActivity [) ]} 

With the modules out of the way, create a component interface to orchestrate the injection
next. The modules are listed in @Component next to the annotation
AndroidSupportInjectionModule . For this example, a common Builder class is annotated with
@ Component.Builder is used to simplify the setup. @Component ( modules = [
AndroidSupportInjectionModule ] :: class
InjectorModule
:: class
AppModule :: class
])
AppComponent . AndroidInjector < NerdApplication > {
@Component Builder
abstract [19659060] [19659000]] 19659055]]

Last but not least, a device application subclass is required to initialize the component. For simplicity,
the class DaggerApplication is expanded, and an override of the function applicationInjector is
implemented to return the generated DaggerAppComponent class. In this example, the component
the class can be initialized in the function applicationInjector and returned. Instead, it is
component is assigned to a property and initialized in onCreate . This is because the test
the configuration must transfer the injection property into the tests to check which one

NerdApplication was [injector] : [19659017] AndroidInjector < with DaggerApplication > ride fun onCreate () onCreate () {
injector = [19659017] DaggerAppComponent . builds (). (19659016) super super . onCreate fun applicationInjector (): AndroidInjector < with DaggerApplication > {
return [19659017] Injector
}
}

With the NerdApplication class in place (and after registering it in AndroidManifest.xml ),
The setup is complete and the classes can request their dependencies. In the list below is
NerdListActivity class expands DaggerAppCompatActivity that automatically injects someone
properties marked with @Inject . In this case, the NerdStore property is labeled so that
component will create and inject an instance of it into the property. Class NerdListActivity


Source link