2014-08-28 64 views
1

我想测试我的应用程序被渲染游戏框架2.3.x版本正确的模板右模板的渲染,是这样的:类似于rspec的呢如何测试

"Index" should{ 
    "render index template" in new WithApplication{ 
     ... 
     val result = call(controller.index, FakeRequest()) 
     someFunction(result) must render(views.html.index) 
    } 
} 

东西:

response.should render_template("success") 

有没有可能,推荐的方法是什么?

回答

1

对于这样一个简单的控制器函数,我会检查响应的内容与渲染模板的内容。请注意,模板呈现为Html,因此您必须致电toStringResult的内容进行比较。我也想检查Result的内容类型和状态。

"Index" should { 
    "render index template" in new WithApplication { 
     val request = FakeRequest(GET, '/') // I prefer using the router, but it doesn't matter that much. 
     val Some(result) = route(request) 
     val expectedContent = views.html.index().toString 

     contentAsString(result) must equalTo(expectedContent) 
     contentType(result) must equalTo("text/html") 
     status(result) must equalTo(OK) 
    } 
} 
+0

好的,这将工作,谢谢。 – nowxue 2014-08-29 02:18:29