In Arrow Kt Documentation on Dependency Injection, the dependency is defined at the "Edge of the World" or in Android could be an Activity
or a Fragment
. So the given example is as follow:
import Api.*
class SettingsActivity: Activity {
val deps = FetcherDependencies(Either.monadError(), ActivityApiService(this))
override fun onResume() {
val id = deps.createId("1234")
user.text =
id.fix().map { it.toString() }.getOrElse { "" }
friends.text =
deps.getUserFriends(id).fix().getOrElse { emptyList() }.joinToString()
}
}
But now I'm thinking how could the SettingsActivity
in the example could be unit tested
? Since the dependency is created within the activity, it could no longer be changed for testing?
When using some other Dependency Injection
library, this dependency definition is create outside of the class it will be used on. For example in Dagger
, a Module
class is created to define how the objects (dependencies) are created and an @Inject
is used to "inject" the dependency defined inside the module. So now when unit testing the Activity
, I just have to define a different module or manually set the value of the dependency to a mock object.