Я после этой статьи, которая описывает, как достичь инъекции зависимостей в Scala через торт Pattern: http://jonasboner.com/real-world-scala-dependency-injection-di/Scala Dependency Injection с тортом Pattern
Я вроде новичок в Scala, и я признаю, некоторые из них пошел над моей головой, до сих пор я получил следующую работу:
// Setup the component and interface
trait AccountRepositoryComponent {
val accountRepository: AccountRepositoryInterface
trait AccountRepositoryInterface {
def message: String
}
}
// An implementation
trait MyAccountRepositoryComponent extends AccountRepositoryComponent {
object AccountRepository extends AccountRepositoryInterface {
def message: String = "Hello"
}
}
// Object to configure which implementations to use and retrieve them
object ComponentRegistry extends MyAccountRepositoryComponent {
val accountRepository = AccountRepository
}
// Example service using the above
object AccountService {
val repo = ComponentRegistry.accountRepository
def say: String = repo.message
}
println(AccountService.say)
То, что я не в состоянии понять, как я бы сейчас передать в поддельном хранилище для учетной записи службы, скажем, изменить выход на " Test ", а не" Hello "?
Спасибо, поэтому его более смешивание, чем фактическая инъекция? основанный на этом, что было бы лучшим способом издеваться над AccountRepository для теста? (предполагая specs2/Mockito) – matthewrk
Получил это, я пытался издеваться над объектом, если я издеваюсь над «Интерфейсом», но он отлично работает. – matthewrk