Play 2.0中的存根控制器 [英] Stub Controller in Play 2.0

查看:107
本文介绍了Play 2.0中的存根控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从模拟Play [2.0]中的对象不幸的是我没有成功.

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

我有一个使用UserModel的UsersController.

I have a UsersController that uses a UserModel.

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

接下来,抽象控制器

abstract class UsersController extends Controller {

  val userModel: UserModel

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


  // Other methods

}

在路由文件中,我以这种方式调用方法"Hello":

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

POST/hello控制器.Users.sayHello

POST /hello controllers.Users.sayHello

在测试目录中,我使用UserModel模拟创建了UsersController的子类.

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]
}

现在,主要部分.我按照前面提到的Jacob Jacobwater示例创建了一个Spec测试.在为FakeApplication插入参数时,我包括了对UserControllersTest的调用.

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)

UsersSayHelloSpec.scala:20指的是我调用运行方法的行.

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:

参数'additionalPlugins'用于其他Play插件,而'controllers.UserControllersTest'不是Play插件.它是一个控制器.

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

您可以在此处了解有关Play 2插件的信息: http://www.objectify.be/wordpress/?p = 464

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

您是否尝试过以下示例: http://www.playframework.org/documentation/2.0.4/ScalaFunctionalTest 吗?

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

这篇关于Play 2.0中的存根控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆