Tod*_*ers 3 unit-testing playframework-2.0
我的play框架应用程序是scala(而不是Java).我找到了一个页面,描述了如何使用实用程序类play.test.Helpers进行单元测试路由.这个例子是Java,而不是scala.我在scala中编写了测试,但是我收到错误"消息:这不是JavaAction,不能以这种方式调用".
这是我发现的页面,描述了如何在play framework 2.0中测试路由:http: //digitalsanctum.com/2012/05/28/play-framework-2-tutorial-testing/
...这是我试图编写的代码来测试我的应用程序:
package conf
import org.scalatest._
import play.mvc.Result
import play.test.Helpers._
class routeTest extends FunSpec with ShouldMatchers {
describe("route tests") {
it("") {
// routeAndCall() fails. Message: This is not a JavaAction and can't be invoked this way.
val result = routeAndCall(fakeRequest(GET, "/"))
result should not be (null)
}
}
}
Run Code Online (Sandbox Code Playgroud)
问题是因为我的行为是Scala而不是Java?我可以通过Scala控制器对我的路线进行单元测试吗
您应该使用play.api.*Scala代码中的导入.play.*是一个Java api.所以你的代码应该是这样的:
package conf
import org.scalatest._
import org.scalatest.matchers._
import play.api._
import play.api.mvc._
import play.api.test.Helpers._
import play.api.test._
class routeTest extends FunSpec with ShouldMatchers {
describe("route tests") {
it("GET / should return result") {
val result = routeAndCall(FakeRequest(GET, "/"))
result should be ('defined)
}
}
}
Run Code Online (Sandbox Code Playgroud)
甚至更好地使用FlatSpec:
package conf
import org.scalatest._
import org.scalatest.matchers._
import play.api._
import play.api.mvc._
import play.api.test.Helpers._
import play.api.test._
class routeTest extends FlatSpec with ShouldMatchers {
"GET /" should "return result" in {
val result = routeAndCall(FakeRequest(GET, "/"))
result should be ('defined)
}
it should "return OK" in {
val Some(result) = routeAndCall(FakeRequest(GET, "/"))
status(result) should be (OK)
}
}
Run Code Online (Sandbox Code Playgroud)
此外,routeAndCall不返回null.它返回Option[Result],即Some[Result]或None,因此在这种情况下,空检查不起作用.
| 归档时间: |
|
| 查看次数: |
2891 次 |
| 最近记录: |