为Rspec和Capybara测试访问设计配置的消息

Tyl*_*itt 5 rspec ruby-on-rails devise capybara

我正在尝试测试未登录的用户在尝试访问安全内容时是否被定向到设计注册页面.我正在使用RSpec和Capybara进行测试.

为了确保它们到达登录页面,我确保它们最终的页面具有设计登录页面的内容通知哈希.(默认情况下,这是:You need to sign in or sign up before continuing.)

而不是像以下那样编写测试:

page.should have_content "You need to sign in or sign up before continuing."
Run Code Online (Sandbox Code Playgroud)

有没有办法访问配置的消息(以防我以后更改)?就像是:

page.should have_content Devise::Messages.Login_required
Run Code Online (Sandbox Code Playgroud)

dim*_*uch 8

存储设备消息config/locales/devise.*.yml,因此您可以像任何其他翻译一样访问它们:

page.should have_content I18n.t("devise.failure.unauthenticated")
Run Code Online (Sandbox Code Playgroud)