scala spray.io测试框架

时间:2013-11-26 13:38:31

标签: scala testing akka backend

我正在开发一个使用scala spray.io框架和喷涂路由的后端解决方案。这是我在scala的第一个项目,所以请耐心等待。

这是路线特征:

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
        }  
  }

}

这是Actor类

class SomeActor extends Actor with SomeRoute {

  def actorRefFactory = context 

  override def actorRef = this.self

}

我有两个问题要问你:

  • 创建一个额外的slave actor类是否是一个好主意,它将保存“register”消息并处理用户注册
  • 使用Specs2RouteTest给我actorRefFactory而不是实际的actor,那么在这种情况下如何定义actorRef?

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

提前谢谢!

1 个答案:

答案 0 :(得分:1)

最后我在上面的代码中遇到的问题是我必须使用CreateUser操作返回的来自Future的onConplete函数:

  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_             
          }
        }
      }
    }

}