2014-11-14 56 views
1

我在grails項目中撰寫黃瓜測試用例。我想要訪問在message.properties文件中配置的消息在grails黃瓜測試中使用I18N消息

我在message.properties文件中配置的屬性爲:

view.user.page.title = View user 

我有黃瓜一步ViewUserSteps.groovy

Given(~'^I looged in into the app$') { -> 
    //code related to login 
} 
When(~'^I click view users tab"$') { -> 
    // click user tab logic 
} 
Then(~'^I see I am on View User page$') { -> 
    at ViewUserPage 
} 

而且ViewUserPage.groovy是

import geb.Page 
import grails.util.Holders 
class ViewUserPage extends Page{ 
    static url = "${Holders.config.app.url}/users/view" 
    static at = { 
     waitFor(30,2) { 
      title == "Edit user" // this title should be fetched from message.properties file 
     } 
    } 
} 

在這裏,在ViewUserPage我應該能夠獲取message.properties中配置的標題。像g.message(代碼:'view.user.page.title')或其他方式,如果我在message.properties中更改,不需要更改測試用例。有什麼想法嗎?

+0

也許這不是你的問題的答案,但你不應該依賴測試中的messages.properties文件。最好在你的html中添加一些測試相關信息,例如' ...'並檢查是否存在'body.test-edit-user'。 – 2014-11-14 13:28:36

+0

感謝您的評論。你能告訴我爲什麼我們不應該依賴消息外化?我不確定爲特定元素添加一個類並聲明它是否是一個好主意。這裏我的要求是驗證頁面標題。 – 2014-11-15 05:27:52

+0

每次有人更改/更新標題,您的測試將失敗。我相信檢查您的應用程序邏輯是否正確(顯示右頁)更有意義。翻譯的驗證應該由翻譯人員來完成,而不是程序員。最終,如果你真的需要檢查這個標題,最好是我相信檢查用於翻譯目的的「鍵」,而不是翻譯本身。也許你可以在測試階段替換g.message的行爲來始終返回'code'? – 2014-11-15 11:42:36

回答

0

您可以詢問messageSource bean的內容,然後將其與請求的結果進行比較。

這可以是這樣的(與responseData.errors.username是請求(即When步驟的結果):

Then(~'^an error message is shown that the user name is already used$') {-> 
    assert responseData.errors.username.contains (message ('user.username.unique')) 
} 

message是一個小的輔助函數來得到消息的文本:

String message (String code) { 
    getBean ('messageSource').getMessage (code, null, null) 
} 

我們將添加到World對象中,以便它可以自動分步獲得。

+0

感謝Martin Hauner,這工作。 – 2014-11-19 12:32:41