Play 2.0:FakeApplication 调用存根控制器而不是真实控制器 [英] Play 2.0: FakeApplication calling a stub controller instead real one
问题描述
这是我看到的先前问题的延续我对 FakeApplication 中插件的使用有误解.
This is a continuation of a prior question in which answer I saw that I had a misconception about the use of plugins in FakeApplication.
首先,我创建了一个模型作为特征和实现它的对象.
First I have a Model created as a trait and a object that implment it.
trait UserModel extends ModelCompanion[User, ObjectId] {
// ...
}
object User extends UserModel
接下来,我有一个控制器,它被创建为一个抽象类,它接收一个 UserModel 的实例,以及它各自使用 User 对象的实现.
Next, I have a Controller, created as a abstract class that receive a instance of UserModel, and its respective implementation that uses the User object.
abstract class UsersController extends Controller {
val userModel: UserModel
def sayHello = Action(parse.json) { request =>
// return a play Action. It doesn't use userModel
}
// Other methods
}
object Users extends UsersController(User)
在测试目录中,我使用 UserModel 模拟创建了一个 UsersController Stub:
In test directory, I created a UsersController Stub using a UserModel mock:
package controllers
import org.specs2.mock.Mockito
object UserControllersTest extends UsersController with Mockito {
val userModel = mock[models.UserModel]
}
现在我有了我的 UsersControllers Spec 测试:
Now I have my UsersControllers Spec test:
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)
// ***************************************
// It will call UsersControllers.sayHello
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 ...
}
}
}
所以我的问题是:当在 routeAndCall() 调用中调用/hello" URL 时,我如何告诉 FakeApplication 使用 UserControllersTest 而不是真正的 UserControllers 实现?
So my question is: How can I say to FakeApplication to use UserControllersTest instead the real UserControllers implementation when call "/hello" URL, in routeAndCall() calling?
推荐答案
您可能想要测试应用程序的两个不同部分:
There are two different part of your application which you might want to test:
- 控制器本身
- http 路由器
当您测试控制器本身时,您通常会创建一个请求并将其直接传递给控制器方法,该方法将生成答案.然后对答案进行一些验证以验证测试结果.
When you test the controller itself, you typically create a request and you directly pass it to a controller method, which will generate the answer. You then perform some validation of the answer to verify the test result.
当您测试路由器时,您要测试的是请求是否路由到正确的控制器方法:您直接调用 url 并检查结果是否是您期望的控制器的结果.
When you test the router, what you want to test is that the request is routed to the right controller method: you directly call the url and you check that the result is the one you expect from the controller you expected.
你想做什么对我来说没有多大意义:
What you are trying to do does not make much sense to me:
如果在路由/hello 我有一个 dummyController,我会在路由上发帖吗/hello 工作正常吗?
if on route /hello I have a dummyController, will my post to the route /hello works correctly?
由于路由文件被编译成一个实际用作路由器的 Scala 类,如果(我不确定)这样的动态路由功能可用,您将不得不:
Since the routes file is compiled into a Scala class which actually works as a router, if (and I am not sure) such a dynamic routing feature is available in play, you would have to:
- 删除/hello路径上的现有路由
- 将新路由点添加到不同的控制器
这篇关于Play 2.0:FakeApplication 调用存根控制器而不是真实控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!