2014-10-10 73 views
2

我實現授權我的戲框架(2.3.5版本)的應用程序爲每official security documentation擔保特質造成SPEC2單元測試編譯錯誤

trait Secured { 

    def username(request: RequestHeader) = request.session.get(Security.username) 

    def onUnauthorized(request: RequestHeader) = Results.Redirect(routes.Login.index) 

    def withAuth(f: => String => Request[AnyContent] => Result) = { 
    Security.Authenticated(username, onUnauthorized) { user => 
     Action(request => f(user)(request)) 
    } 
    } 

} 

然而,當我裝修我的控制器動作與withAuth功能如下所示:

def export = withAuth { username => implicit request => 
    val csv = csv() 
    Ok(csv) 
    .as("text/csv") 
    .withHeaders(
     CONTENT_DISPOSITION -> ("attachment; filename=export.csv"), 
     CONTENT_LENGTH -> csv.length.toString 
    ) 
} 

我得到的編譯錯誤在我的控制器specs2單元測試:

"export data as CSV" in { 
    val controller = new controllers.DataController 
    val result = controller.export().apply(FakeRequest()) 
    headers(result) must havePair("Content-Disposition" -> ("attachment; filename=export.csv")) 
} 

headers測試輔助函數調用失敗,出現以下編譯器錯誤消息:

類型不匹配;發現:play.api.libs.iteratee.Iteratee [數組[字節],play.api.mvc.Result]要求:scala.concurrent.Future [play.api.mvc.Result]

我做有問題?我嘗試了其他Stackoverflow用戶的一些remediessuggested,但它們似乎都依賴於設置正文類型,但我的操作沒有正文類型。

看起來好像withAuth的呼叫模糊了某些被包裹的動作類型,所以也許有更安全的方式表達Secured特徵?

回答

1

嘗試修改此:

val result = controller.export().apply(FakeRequest()) 

到這一點:

val result = controller.export().apply(FakeRequest()).run 

然後它應該工作。