对于集成测试,我会检查页面内容,以确保用户会看到我想要的内容。
例如:
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/