【问题标题】:Using I18N messages in grails cucumber test在 grails 黄瓜测试中使用 I18N 消息
【发布时间】:2014-11-18 05:19:44
【问题描述】:

我正在 grails 项目中编写黄瓜测试用例。我想访问在 message.properties 文件中配置的消息

例如我在 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(code:'view.user.page.title') 或其他方式,这样如果我在 message.properties 中更改,则无需更改测试用例。有什么想法吗?

【问题讨论】:

  • 也许它不能回答你的问题,但你不应该在测试中依赖 messages.properties 文件。最好在你的html中添加一些测试相关信息,例如<body class="test-edit-user">...</body>并检查body.test-edit-user是否存在。
  • 感谢您的评论。你能告诉我为什么我们不应该依赖消息外部化吗?我不确定将一个类添加到特定元素并断言它是一个好主意。这里我的要求是验证页面标题。
  • 每次有人更改/更新标题,您的测试都会失败。我相信检查您的应用程序逻辑是否正确(显示正确的页面)更有意义。翻译的验证应该由翻译人员完成,而不是程序员。最后,如果你真的需要检查这个标题,我相信最好检查用于翻译目的的key,而不是翻译本身。也许您可以在测试阶段替换 g.message 的行为以始终返回code

标签: grails groovy cucumber geb


【解决方案1】:

您可以询问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)
}

我们将message 添加到World 对象中,以便它可以分步自动使用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-28
    • 2012-01-18
    • 2013-05-23
    相关资源
    最近更新 更多