Stub Controller in Play 2.0

♀尐吖头ヾ 提交于 2019-12-25 14:23:53

问题


I am trying folow the example from Mock Objects in Play[2.0] but unfortunately I am not having success.

I have a UsersController that uses a UserModel.

trait UserModel extends ModelCompanion[User, ObjectId] {
 // ...
}

Next, the abstract controller

abstract class UsersController extends Controller {

  val userModel: UserModel

  def sayHello = Action(parse.json) { request =>
    // return a play Action. Doesn't use userModel
  }


  // Other methods

}

In the routes file, I call method say Hello in this way:

POST /hello controllers.Users.sayHello

In test directory, I created a subclass of UsersController using a UserModel mock.

package controllers

import org.specs2.mock.Mockito

object UserControllersTest extends UsersController with Mockito {
  val userModel = mock[models.UserModel]
}

Now, the main part. I created a Spec test following the Jacob Groundwater example in the page mentioned before. In pluing argument for FakeApplication, I included a calling to UserControllersTest.

package controllers

import org.specs2.mutable.Specification

import play.api.libs.json.Json
import play.api.test._
import play.api.test.Helpers._

class UsersSayHelloSpec extends Specification {

  running(FakeApplication()) {

    "Users.SayHello" should {

      def sendJson(jsonMap: Map[String, String], shouldBeCorrect: Boolean) = {
        running(new FakeApplication(
          additionalPlugins = Seq("controllers.UserControllersTest"))) {
          // Preapration 
          val jsonRequisition = Json.toJson(jsonMap)
          val Some(result) = routeAndCall(FakeRequest(POST,
              "/hello",
              FakeHeaders(Map("Content-Type" -> Seq("application/json"))),
              jsonRequisition))

            // ...
        }
      }

      "Not process a empty String" in {
        sendJson(Map.empty[String, String], false)
      }

      // Other tests calling sendJson ...
    }

  }

}

However, when I run the test I got this error message:

[info] Users.SayHello should
[error] ! Not process a empty String
[error]     PlayException: Cannot load plugin [Plugin [controllers.UserControllersTest] cannot been instantiated.] (Application.scala:171)
...
[error] play.api.Application.<init>(Application.scala:158)
[error] play.api.test.FakeApplication.<init>(Fakes.scala:141)
[error] controllers.UsersSayHelloSpec$$anonfun$1$$anonfun$apply$5.sendJson$1(UsersSayHelloSpec.scala:20)
[error] controllers.UsersSayHelloSpec$$anonfun$1$$anonfun$apply$5$$anonfun$apply$26.apply(UsersSayHelloSpec.scala:46)
[error] controllers.UsersSayHelloSpec$$anonfun$1$$anonfun$apply$5$$anonfun$apply$26.apply(UsersSayHelloSpec.scala:46)

Where UsersSayHelloSpec.scala:20 referes to line where I call running method.

So my question is: What am I doing wrong?


回答1:


I'm not sure what exactly are you trying to do, but the answer for question 'What am I doing wrong?' is:

The parameter 'additionalPlugins' is for additional Play plugins, and 'controllers.UserControllersTest' is not a Play plugin. It's a Controller.

You can read about Play 2 plugins here: http://www.objectify.be/wordpress/?p=464

Have you tried these examples: http://www.playframework.org/documentation/2.0.4/ScalaFunctionalTest ?



来源:https://stackoverflow.com/questions/13284579/stub-controller-in-play-2-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!