基本设计规格与I18N [英] Basic Devise spec with I18N
问题描述
描述OrgsController do
描述没有登录时的GET索引do
it重定向到new_user_session_pathdo
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用户
结束
#剩余规格
结束
我得到预期的回复是重定向到< http://test.host/users/sign_in?locale = en>但是重定向到< ; http://test.host/users/sign_in> ;.
我已经实现了i18n,并在我的应用程序控制器中: / p>
before_filter:set_i18n_locale_from_params
通过以下方式获得这项工作的最佳方式是什么?
- 获取路线匹配?
- 对用户进入和退出之间进行重新签名以达到前面(每个)块的效果的黑客?
- 整体方法?
获取控制器测试/规范正确设置区域设置的问题是一个长期的问题,请参见: a href =http://stackoverflow.com/questions/1987354/how-to-set-locale-default-url-options-for-func tional-tests-rails>如何设置功能测试(Rails)的区域设置default_url_options
从你写的内容看起来像 get:index
默认情况下不设置区域设置,因为 default_url_options
将无法从控制器测试/规范中工作。尝试上面链接中提到的解决方案之一,以获得匹配的路由。另见关于github的这个讨论: https://github.com/rspec/rspec-rails/问题/ 255
为什么值得,这是补丁我目前在规范/测试中使用的语言环境自动从当前语言环境传递到路由:
pre $
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
关于之前(每个)
块的设置,我真的建议使用上下文块分离案例,一个上下文没有之前过滤器记录用户,另一个用前面的块记录用户。设置你现在对于试图看看发生了什么的人真的很困惑。
I'm new to RSpec, and trying to write a simple test that shows Devise is working. I've picked a random page and want to write a test that shows that a non logged-in user is re-redirected to /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
I get "Expected response to be a redirect to <http://test.host/users/sign_in?locale=en> but was a redirect to <http://test.host/users/sign_in>."
I have implemented i18n, and have in my Application Controller:
before_filter :set_i18n_locale_from_params
What is the best way to get this working in terms of:
- Getting the routes to match?
- The hackishness of signing a user in and out again to get round the effects of the before(:each) block?
- The overall approach?
The problem of getting controller tests/specs to properly set the locale is a longstanding one, see: How to set locale default_url_options for functional tests (Rails)
From what you've written, it looks like get :index
is not setting the locale by default, because default_url_options
will not work from controller tests/specs. Try one of the solutions mentioned in the link above to get the routes to match. See also this discussion on github: https://github.com/rspec/rspec-rails/issues/255
For what it's worth, this is the patch I currently use in specs/tests to get the locale to be automatically passed to routes from the current locale:
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
Regarding the hackishness of the before(:each)
block setup you have, I'd really suggest separating the cases using context blocks, with one context without the before filter logging the user in, and another with the before block logging the user in. The setup you have right now is really confusing for someone trying to see what's going on.
这篇关于基本设计规格与I18N的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!