Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unit testing 使用SecureSocial在Play框架中对控制器进行单元测试_Unit Testing_Scala_Playframework 2.0_Securesocial - Fatal编程技术网

Unit testing 使用SecureSocial在Play框架中对控制器进行单元测试

Unit testing 使用SecureSocial在Play框架中对控制器进行单元测试,unit-testing,scala,playframework-2.0,securesocial,Unit Testing,Scala,Playframework 2.0,Securesocial,我正在尝试发明某种模拟SecureSocial动作生成器,或者SecureSocial本身,以便能够对控制器方法进行单元测试。 我发现了一些方法,比如,但问题是,在这个问题中,作者实际上不做单元测试,而是做集成测试 我的单元测试如下所示: trait MockDaoProvider extends IDaoProvider { def entityDao = entityDaoMock } val controller = new MyController with Moc

我正在尝试发明某种模拟SecureSocial动作生成器,或者SecureSocial本身,以便能够对控制器方法进行单元测试。 我发现了一些方法,比如,但问题是,在这个问题中,作者实际上不做单元测试,而是做集成测试

我的单元测试如下所示:

  trait MockDaoProvider extends IDaoProvider {
    def entityDao = entityDaoMock
  }

  val controller = new MyController with MockDaoProvider

  "MyController.list" should {
    "return an OK" in {
      entityDaoMock.list().returns(List())

      val result = controller.list()(FakeRequest())

      status(result) must equalTo(OK)
    }
  }
正如大家所看到的,我模拟依赖项来隔离和测试控制器方法实际执行的行为

在我使用securesocial中的SecuredAction for MyController.list方法之前,一切都正常。现在我得到一个异常,测试失败了。我不知道如何从securesocial中模拟、存根或重写SecuredAction和UserAwareAction对象。我仍然不想将我的测试转换为路由(…)测试。它们仅用于测试控制器的行为

有人遇到过同样的问题吗?可能有什么线索可以解决吗


PS:Play framework 2.2.1,securesocial-2.1.2

代码的作者似乎没有强调可测试性,这迫使用户提出自己的新颖解决方案。用户jeantil可能会有所帮助:

class FakeAuthenticatorStore(app:Application) extends AuthenticatorStore(app) {
  var authenticator:Option[Authenticator] = None
  def save(authenticator: Authenticator): Either[Error, Unit] = {
    this.authenticator=Some(authenticator)
    Right()
  }
  def find(id: String): Either[Error, Option[Authenticator]] = {
    Some(authenticator.filter(_.id == id)).toRight(new Error("no such authenticator"))
  }
  def delete(id: String): Either[Error, Unit] = {
    this.authenticator=None
    Right()
  }
}

abstract class WithLoggedUser(val user:User,override val app: FakeApplication = FakeApplication()) extends WithApplication(app) with Mockito{
  lazy val mockUserService=mock[UserService]
  val identity=IdentityUser(Defaults.googleId, user)

  import helpers._
  import TestUsers._
  def cookie=Authenticator.create(identity) match {
    case Right(authenticator) => authenticator.toCookie
  }

  override def around[T: AsResult](t: =>T): execute.Result = super.around {
    mockUserService.find(Defaults.googleId) returns Some(identity)
    UserService.setService(mockUserService)
    t
  }
}

  val excludedPlugins=List(
    ,"service.login.MongoUserService"
    ,"securesocial.core.DefaultAuthenticatorStore"
  )
  val includedPlugins = List(
    "helpers.FakeAuthenticatorStore"
  )

  def minimalApp = FakeApplication(withGlobal =minimalGlobal, withoutPlugins=excludedPlugins,additionalPlugins = includedPlugins)
这样就可以进行这样的测试

"create a new user password " in new WithLoggedUser(socialUser,minimalApp) {
  val controller = new TestController
  val req: Request[AnyContent] = FakeRequest().
    withHeaders((HeaderNames.CONTENT_TYPE, "application/x-www-form-urlencoded")).
    withCookies(cookie) // Fake cookie from the WithloggedUser trait

  val requestBody = Enumerator("password=foobarkix".getBytes) andThen Enumerator.eof
  val result = requestBody |>>> controller.create.apply(req)

  val actual: Int= status(result)
  actual must be equalTo 201
}

经过一些思考、探索和实验,我终于找到了一个优雅的解决方案。该解决方案依赖于依赖项注入的“蛋糕模式”。像这样:

控制器中的代码:

trait AbstractSecurity {
  def Secured(action: SecuredRequest[AnyContent] => Result): Action[AnyContent]
}

trait SecureSocialSecurity extends AbstractSecurity with securesocial.core.SecureSocial {
   def Secured(action: SecuredRequest[AnyContent] => Result): Action[AnyContent] = SecuredAction { action }
}

abstract class MyController extends Controller with AbstractSecurity {

  def entityDao: IEntityDao

  def list = Secured { request =>
    Ok(
      JsArray(entityDao.list())
    )
  }
}

object MyController extends MyController with PsqlDaoProvider with SecureSocialSecurity
和测试代码:

 trait MockedSecurity extends AbstractSecurity {
    val user = Account(NotAssigned, IdentityId("test", "userpass"), "Test", "User",
      "Test user", Some("test@user.com"), AuthenticationMethod("userPassword"))

    def Secured(action: SecuredRequest[AnyContent] => play.api.mvc.Result): Action[AnyContent] = Action { request =>
      action(new SecuredRequest(user, request))
    }
  }


  val controller = new MyController with MockDaoProvider with MockedSecurity

  "IssueController.list" should {
    "return an OK" in {
      entityDaoMock.list().returns(List())

      val result = controller.list()(FakeRequest())

      status(result) must equalTo(OK)
    }
  }
但也有一个缺点——考试也取决于社会阶层。。。但是这真的是一个缺点吗?
我不知道这种方法在更复杂的情况下如何工作,我们拭目以待。

我说得再好不过了:)我只是希望我公正地对待了你,哈哈。