2013-11-26 57 views
3

我正在開發一個使用scala spray.io框架和噴霧路由的後端解決方案。這是我在斯卡拉的第一個項目,請耐心等待。scala spray.io測試框架

這裏是路線特點:

trait SomeRoute extends HttpService { 

    implicit def executionContext = actorRefFactory.dispatcher 

    def actorRef 

    val route = { 
    post { 
     path("register/") { 
     ask(actorRef, CreateUser , 5.seconds).mapTo[ User ].onComplete { 
      render response here 
     } 
    } 

} 

這裏是演員類

class SomeActor extends Actor with SomeRoute { 

    def actorRefFactory = context 

    override def actorRef = this.self 

} 

我有2個問題給你:

  • 難道是要創造一個好主意一個額外的從屬演員類,它將保存「註冊」消息並處理用戶註冊
  • 使用Specs2RouteTest給了我actorRefFactory但不是實際的actor,所以在這種情況下如何定義actorRef?

    class SomeRouteSpec extends Specification with Actor with Specs2RouteTest with SomeRoute{ 
    
        def actorRefFactory = system 
    
        override def actorRef = ??? 
    
    } 
    

預先感謝您!

回答

1

最後在上面,我不得不從將來使用onConplete功能代碼的問題通過返回操作CREATEUSER:

path("register/") { 
    entity(as[ UserRegisterRequest ]) { 
     userRegInfo => ctx => 
     registerUser(userRegInfo, ctx.complete) 
    } 
    }  

    def registerUser( 
        userReg: UserRegisterRequest, 
        renderResponse:RenderResponse): Unit = { 

     usersOps.createUser(userReg, 
          DateTime.now.plus( 
          Period.days(vcsettings.passExpired))) 
     .onComplete { 
     (tryObj) => renderResponse { 
     tryObj match { 
      case Success(user) => VcResponse("register_user:", 
      Map("user:" -> user.get.id))   

      case Failure(e) => { 
      logger < "can't register user: $userReg.toString" 
      _error_getting_user_messages_    
      } 
     } 
     } 
    } 

}