我有服务:
trait MyService extends HttpService {
def getDao(implicit dao: SomeDAO) = dao
def someRoute = path("foo") {
get {
complete(getDao getSomething)
}
}
}
然后,我有一个演员:
class MyActor extends MyService with Actor {
override def receive: Receive = runRoute(someRoute)
def actorRefFactory: ActorRefFactory = context
}
我的测试类看起来像这样:
class MyServiceTest extends FlatSpec with ScalatestRouteTest with MyService with Matchers with MockFactory {
override implicit def actorRefFactory: ActorSystem = system
implicit val _dao: SomeDAO = mock[SomeDAO]
"My service" should "return something" in {
Get("/foo") ~> someRoute ~> check {
status should be(OK)
}
}
}
但是当我运行测试时,编译器会抱怨无法找到SomeDAO
的隐含值。如何设法将SomeDAO
纳入我的服务?我错过了什么/我做错了什么?
答案 0 :(得分:1)
我认为您最好将implicit
声明为someRoute
,如下所示:
trait MyService extends HttpService {
def someRoute(implicit dao: SomeDAO) = path("foo") {
get {
complete(dao getSomething)
}
}
}
它应该编译,并且使用一个方法来检索隐式更有意义。
答案 1 :(得分:0)
ScalatestRouteTest已经提供了一个隐含的ActorySystem。从actorRefFactory方法中删除“隐式”修饰符,测试应该执行。
这为我的代码解决了这个问题