Basic Devise spec with I18N

后端 未结 1 1821
伪装坚强ぢ
伪装坚强ぢ 2021-01-16 01:41

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

相关标签:
1条回答
  • 2021-01-16 02:46

    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.

    0 讨论(0)
提交回复
热议问题