rspec 测试国际化观点?

发布于 2024-08-11 21:50:51 字数 516 浏览 2 评论 0原文

我想确保我的视图中有正确的元描述/关键字和标题文本,因此我想为此创建 rspec 视图测试。现在真正的挑战是如何让它跨多种语言工作。

我这样做的方式是:

it "should have the right page title" do
  title = "some nice title here"
  response.should have_tag("title", title)
end

所以因为“要求”在该示例中是硬编码的,所以我很难弄清楚如何为我的 config/locale/ 中的所有其他语言做同样的事情。

我不确定这是否是最好的方法,或者我应该像这样从 locale/lang.yml 中获取文本:

it "should have the right page title" do
  title = t('site.title')
  response.should have_tag("title", title)
end

谢谢

I want to make sure I have the right meta desc/keyword and title text in my view so I want to create rspec views tests for that. Now the real challange here is how to make it work across multiple languages.

The way I've done it is:

it "should have the right page title" do
  title = "some nice title here"
  response.should have_tag("title", title)
end

So because the "requirement" is hard coded in that example, I am having a hard time figuring out how to do the same thing for all the other languages in my config/locale/.

I'm not sure if this is the best way to do it or should I just fetch the text from the locale/lang.yml like so :

it "should have the right page title" do
  title = t('site.title')
  response.should have_tag("title", title)
end

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜未央樱花落 2024-08-18 21:50:51

这似乎完全可以接受。我认为没有理由直接从本地化文件中获取。

它应该给您一个很好的指示,表明您确实在整个应用程序中使用本地化调用。您甚至可以在不同的区域设置下运行测试,这将帮助您确保为每个区域设置填写了所有密钥。

我觉得它与使用固定装置进行测试没有任何不同,它仍然要求您的 .yml 文件正确,并且不会检查以确保您已正确创建它们,但它确实可以帮助您检查是否已正确创建它们在您的代码中正确使用它们。

This seems perfectly acceptable. I see no reason to fetch directly from the localization files.

It should give you a good indication that you are indeed using the localized calls throughout your application. You could probably even run your tests under the different locales and that would help you ensure you have all the keys filled out for each one.

It doesn't strike me as any different than testing using fixtures, it still requires that your .yml file be correct, and won't check to make sure you've created them correctly, but it does help you check that you've properly used them in your code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文