[英]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. 我正在尝试从Play [2.0]中的Mock Objects中获得该示例,但是不幸的是我没有成功。
I have a UsersController that uses a UserModel. 我有一个使用UserModel的UsersController。
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: 在路由文件中,我以这种方式调用方法说Hello:
POST /hello controllers.Users.sayHello POST / hello控制器.Users.sayHello
In test directory, I created a subclass of UsersController using a UserModel mock. 在测试目录中,我使用UserModel模拟创建了UsersController的子类。
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. 我按照前面提到的Jacob Jacobwater示例创建了一个Spec测试。 In pluing argument for FakeApplication, I included a calling to UserControllersTest. 在为FakeApplication插入参数时,我包括了对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. UsersSayHelloSpec.scala:20指向我在其中调用运行方法的行。
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. 参数“ additionalPlugins”用于其他Play插件,而“ controllers.UserControllersTest”不是Play插件。 It's a Controller. 它是一个控制器。
You can read about Play 2 plugins here: http://www.objectify.be/wordpress/?p=464 您可以在此处阅读有关Play 2插件的信息: http ://www.objectify.be/wordpress/?p= 464
Have you tried these examples: http://www.playframework.org/documentation/2.0.4/ScalaFunctionalTest ? 您是否尝试过以下示例: http : //www.playframework.org/documentation/2.0.4/ScalaFunctionalTest ?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.