2014-08-28 55 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