我试图使用play framework版本2.5.9对控制器进行功能测试。
我有一个名为HomeController的控制器
@Singleton
class HomeController @Inject()(implicit exec: ExecutionContext) extends Controller {
def index = Action.async {
Future{
Ok(views.html.index("Home"))
}
}
}
我的观点看起来像这样
@(message: String)
@main("Home") {
<h2>Welcome</h2>
}
@main中的参数是页面标题中的内容 我的测试看起来像这样
class ApplicationTwoSpec extends PlaySpec with OneServerPerSuite with OneBrowserPerSuite with HtmlUnitFactory{
implicit val ec = Implicits.global
val homeController = new HomeController()
implicit override lazy val app =
new GuiceApplicationBuilder()
.router(Router.from{
case GET(p"/") => homeController.index
})
.build()
"The sample Controler with a server" must {
"have home in the title" in {
go to s"http://localhost:9000/"
pageTitle mustBe "Home"
}
}
}
当我运行测试时,我回来了
must return and ok response back *** FAILED ***
"[]" was not equal to "[Home]" (ApplicationTwoSpec.scala:37)
我需要做些什么来让它得到适当的回应?
答案 0 :(得分:0)
我在找到scala test plus和playframework的源代码后发现了问题。
go to s"http://localhost:9000/"
应该是
go to s"http://localhost:$port/"
在implicit lazy val port
特征中指定OneServerPerSuite
并从play.api.test.Helpers包中获取其默认值,默认为端口19001.如果我真的需要端口为9000我可以也可以使用
implicit override lazy val port = 9000