ご指導ありがとうございました!
SecureSocial プラグインは、ブラウザから実行すると正常に動作しますが、Play アプリの残りの部分を今すぐテストできるようにしたいと考えています。
クイックイントロ
SecureSocial の構文は次のようになります。
def page = SecuredAction(WithProvider("google")) { ... }
またはこれ:
def page = UserAwareAction { ... }
これは、SecureSocial に関する私の問題にリモートで関連していても、スタック オーバーフローに関する唯一の質問のようですが、バイトコードを再配線することはあまり好きではありません。これにはもっと簡単な解決策があるはずです。
SecureSocial で保護されたアクションにアクセスするテストを実行しているときに、大きなエラーが発生しました。これは基本的に、ユーザーに渡していないことを意味していると推測されます。(この質問の下部を参照)
やりたいこと
すべての関数を注入して型を返すAction
代わりに、SecuredAction
またはUserAwareAction
テスト中にのみ型を返す
または、実際にテスト ユーザーを呼び出しに渡します。しかし、どのように?
私が持っているもの
@Singleton
class JsonOps @Inject() () extends Controller with SecureSocial {...}
ここと私のテストでGlobal.scala
説明されているように書かれています...
val controller = new JsonOps
val result = controller.userAwareActionRequestForSomeJson("")(FakeRequest())
次のような電話もあります。
// This is what I would use for production
def extjs = SecuredAction(WithProvider("google")) { implicit request =>
Ok(views.html.extjs(request.user.firstName))
}
// This is what I would use for testing
def extjs = Action { implicit request =>
Ok(views.html.extjs("testtesttesting"))
}
この問題が依存性注入に適していると思う理由は何ですか? ただし、使用している Global.scala はジェネリック クラスのインスタンス化子であるため、クラスのインスタンス化をどのように行うかはわかりません。私が持っているコントローラーごとに9000以上の特性を書きたいとは特に思っていません。
大きな間違い
これは UserOpsSpec.scala の 12 行目と 13 行目です。
12 val controller = new UserOps
13 val result = controller.extjs()(FakeRequest())
これがエラーです
[error] RuntimeException: java.lang.ExceptionInInitializerError (UserOpsSpec.scala:13)
[error] play.api.mvc.ActionBuilder$$anon$1.apply(Action.scala:220)
[error] securesocial.core.SecureSocial$.authenticatorFromRequest(SecureSocial.scala:200)
[error] securesocial.core.SecureSocial$$anonfun$SecuredAction$1.apply(SecureSocial.scala:81)
[error] securesocial.core.SecureSocial$$anonfun$SecuredAction$1.apply(SecureSocial.scala:78)
[error] play.api.mvc.ActionBuilder$$anon$1.apply(Action.scala:215)
[error] play.api.Play$$anonfun$current$1.apply(Play.scala:51)
[error] play.api.Play$$anonfun$current$1.apply(Play.scala:51)
[error] play.api.Play$.current(Play.scala:51)
[error] securesocial.core.Authenticator$.cookieName$lzycompute(Authenticator.scala:188)
[error] securesocial.core.Authenticator$.cookieName(Authenticator.scala:188)
[error] securesocial.core.Authenticator$.<init>(Authenticator.scala:201)
[error] securesocial.core.Authenticator$.<clinit>(Authenticator.scala)
[error] securesocial.core.SecureSocial$.authenticatorFromRequest(SecureSocial.scala:200)
[error] securesocial.core.SecureSocial$$anonfun$SecuredAction$1.apply(SecureSocial.scala:81)
[error] securesocial.core.SecureSocial$$anonfun$SecuredAction$1.apply(SecureSocial.scala:78)
[error] play.api.mvc.ActionBuilder$$anon$1.apply(Action.scala:215)
[error] null
[error] securesocial.core.SecureSocial$.authenticatorFromRequest(SecureSocial.scala:200)
[error] securesocial.core.SecureSocial$$anonfun$SecuredAction$1.apply(SecureSocial.scala:81)
[error] securesocial.core.SecureSocial$$anonfun$SecuredAction$1.apply(SecureSocial.scala:78)
[error] play.api.mvc.ActionBuilder$$anon$1.apply(Action.scala:215)
[error] There is no started application
[error] play.api.Play$$anonfun$current$1.apply(Play.scala:51)
[error] play.api.Play$$anonfun$current$1.apply(Play.scala:51)
[error] play.api.Play$.current(Play.scala:51)
[error] securesocial.core.Authenticator$.cookieName$lzycompute(Authenticator.scala:188)
[error] securesocial.core.Authenticator$.cookieName(Authenticator.scala:188)
[error] securesocial.core.Authenticator$.<init>(Authenticator.scala:201)
[error] securesocial.core.Authenticator$.<clinit>(Authenticator.scala)
[error] securesocial.core.SecureSocial$.authenticatorFromRequest(SecureSocial.scala:200)
[error] securesocial.core.SecureSocial$$anonfun$SecuredAction$1.apply(SecureSocial.scala:81)
[error] securesocial.core.SecureSocial$$anonfun$SecuredAction$1.apply(SecureSocial.scala:78)
[error] play.api.mvc.ActionBuilder$$anon$1.apply(Action.scala:215)
[info]