I18N的基本设计规范

我是RSpec的新手,并尝试编写一个简单的测试,显示Devise正在工作。 我选择了一个随机页面,想要编写一个测试,显示未登录的用户被重定向到/ users / sign_in。

describe OrgsController do describe "GET index when not logged in" do it "redirects to new_user_session_path" do user = FactoryGirl.create(:user) user.confirm! sign_in user sign_out user get :index response.should redirect_to new_user_session_path end end before(:each) do user = FactoryGirl.create(:user) user.confirm! sign_in user end # remaining specs end 

我得到"Expected response to be a redirect to but was a redirect to ."

我已经实现了i18n,并且在我的应用程序控制器中:

 before_filter :set_i18n_locale_from_params 

在以下方面实现这一目标的最佳方法是什么:

  • 让路线匹配?
  • 为了绕过前(:每个)块的影响而进入和退出用户的hackishness?
  • 整体方法?

获取控制器测试/规范以正确设置语言环境的问题是一个长期问题,请参阅: 如何为function测试(Rails)设置语言环境default_url_options

根据您编写的内容,它看起来像get :index默认情况下不设置语言环境,因为default_url_options不能用于控制器测试/规范。 尝试上面链接中提到的解决方案之一,以获得匹配的路由。 另见关于github的讨论: https : //github.com/rspec/rspec-rails/issues/255

对于它的价值,这是我目前在规范/测试中使用的补丁 ,以使区域设置自动从当前区域设置传递到路由:

 class ActionDispatch::Routing::RouteSet def url_for_with_locale_fix(options) url_for_without_locale_fix(options.merge(:locale => I18n.locale)) end alias_method_chain :url_for, :locale_fix end 

关于你拥有的before(:each)块设置的hackishness,我真的建议使用上下文块分隔这些案例,一个上下文没有使用filter记录用户,另一个使用before块记录用户。你现在的设置对于试图了解正在发生的事情的人来说真的很困惑。