简体   繁体   中英

Stub Controller in Play 2.0

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?

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 ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM