Scala依赖注入和测试

时间:2017-07-28 03:27:19

标签: scala dependency-injection akka

我在以下场景中查看替代方案(构造函数参数,DI等)。我有一个以简单数据库条目开头的集成测试,然后测试ETL管道来转换这些条目。然后,根据实际程序,在ETL类树中需要数据库连接,但在集成测试类中也需要输入原始数据。我试图提供单独的数据库访问测试类和etl,但后来我得到锁定错误。我正在努力寻找最佳方式

快速单元测试如下所示:

class myTest extends TestLibrary with DbTestSupport {
   db.find("foo")... 
}
trait DbTestSuport {
 //setup
 val db = ExternalLibrary.database(port=800).open
}

但是现在我想用模拟/测试DB进行完整的集成测试,我不得不面对各种继承。

class myFunctionalTest extends TestLibrary {
  //somehow get access to db
  def addSomeTestData = db.add(it: List[Foo])
  val items = List(Foo(1), Foo(2))
  addSomeTestData(items)
 //now lets test an ETL pipeline

 def transform = new Transform().methodThatDoesThings...//
 transform.testMethodToExpectThings//

}

需要进一步向下db访问:

class Transform {
  def methodThatDoesThings = {
   stream.via(new CoolStreamMethod)
  //...
}
class CoolStreamMethod {
 //setup
 //**somehow need access to DAO
 dao.myquery map { results =>
  process(results)
 }
}
class ActorDao {
 val actor = //create actor router which forwards to workers
 def myquery = actor ? CoolQuery
}
class Worker extends Actor {
 dao = //Get access to a DAO implementation
 def receive = {
  case q: CoolQuery => dao.find(q.thing)
}
class DaoImpl {
 def find(item: Bar) = db.find("item.id") // **database specific stuff where ExternalLibrary.database method needed as in original unit test
}

0 个答案:

没有答案