2015-01-14 29 views
10

我有以下服務:Spray.io:不能編譯測試規範

trait PingService extends MyHttpService { 

val pingRoutes = 
    path("ping") { 
     get { 
     complete("message" -> "pong") 
     } 
    } 
} 

MyHttpService是擴展HttpService自定義類,只包含的實用方法。

這是測試規範:

import akka.actor.ActorRefFactory 
import org.json4s.{DefaultFormats, Formats} 
import org.scalatest.{FreeSpec, Matchers} 
import spray.testkit.ScalatestRouteTest 

class PingServiceSpec extends FreeSpec with PingService with ScalatestRouteTest with Matchers { 

override implicit def actorRefFactory: ActorRefFactory = system 

override implicit def json4sFormats: Formats = DefaultFormats 

    "Ping service" - { 
    "when calling GET /ping" - { 
     "should return 'pong'" in { 
     Get("/ping") ~> pingRoutes ~> check { 
      status should equal(200) 
      entity.asString should contain("pong") 
     } 
     } 
    } 
    } 
} 

每當我試着運行測試,我得到以下錯誤:

could not find implicit value for parameter ta: PingServiceSpec.this.TildeArrow[spray.routing.RequestContext,Unit] 

Get("/ping") ~> userRoutes ~> check { 
      ^

我做一些愚蠢的事?任何形式的幫助將不勝感激!

編輯:雖然這可能看起來像是this question,但事實並非如此。

該解決方案提供的解決方案不起作用。

回答

19

ScalatestRouteTest已經提供了隱含的ActorSystem。從actorRefFactory方法中刪除implicit修飾符,並且應該執行測試。

+1

這解決了這個問題!謝謝! –

+0

你介意解釋一下嗎? :( – Observer

+0

上述錯誤是由於隱含歧義造成的,因爲您在作用域中有兩個隱式的「ActorRefFactory」,一個來自['ScalatestRouteTest'](http://spray.io/documentation/1.1-SNAPSHOT/ api/index.html#spray.testkit.RouteTest)'PingServiceSpec'基於並明確聲明爲'actorRefFactory'。 – edi