ruby-on-rails - 我应该将 View 字符串放入 yaml 文件以便于测试吗?

标签 ruby-on-rails testing internationalization integration-testing

对于集成测试,我会检查页面内容,以确保用户会看到我想要的内容。

例如:

it "shows list of articles" do
    get :articles
    response.body.should have_content("Articles found:")
end

在文章 View 的某处,有这一行:

<h1>Articles found:</h1>

我想摆脱字符串的口是心非,让代码更容易维护,测试更可靠。我正在考虑将这些字符串放入 config/locales/en.yml 中,然后执行类似的操作

it "shows list of articles" do
    get :articles
    response.body.should have_content(I18n.t('title'))
end

在 View 中:

<h1><%=t :title %></h1>

从长远来看是否有意义,或者是否有更好/标准化的方法?

最佳答案

我不会,国际化字符串是供人类阅读的。

作为替代方案,看看您能否以一种使通过选择断言更具确定性的方式构建文档。

it "shows list of articles" do
    get :articles
    assert_select "h1.articles-title"
end

在 View 中。

<h1 class="articles-title"><%=t :title %></h1>

虽然我知道您的原始测试用例断言存在正确的标题,但我认为像这样的一般集成测试的最大好处是存在标题

关于ruby-on-rails - 我应该将 View 字符串放入 yaml 文件以便于测试吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13706670/

相关文章:

ruby-on-rails - "bash: bundle: command not found"- 上限中止

ruby-on-rails - 从 AWS Elastic Transcoder 作业中检索文件和缩略图 URL

ruby-on-rails - ruby ,ActiveRecord : Local rails server updates record attributes whereas the remote server does not

ruby-on-rails - 如何运行 GenieACS?

c++ - I18n C++ 复数形式的 hello world

java - 使用多线程测试 Java 套接字通信

ruby-on-rails - 如何测试 Rails 应用程序在生产环境中是否正常工作

maven - 如何使用 'mvn test' 运行 Cucumber 测试

java - 用于检测国际化问题的静态分析工具

internationalization - 使用众包的应用程序国际化/翻译经验?机械土耳其人?