Search code examples
ruby-on-railsrubyrspecinternationalizationrails-i18n

Controller HTTP methods not working with rspec due to I18n.locale error?


So I'm trying to test controller methods with rspec tests, but they aren't working due to some issue with I18n? I'm getting this error message:

masonscott@amory:~/Workspace/cs169/cs169-smileygo$ rspec spec/controllers/reviews_controller_spec.rb 
F..

Failures:

  1) ReviewsController POST #create Review can be created
     Failure/Error: I18n.locale = env.http_accept_language.preferred_language_from(available)

     NoMethodError:
       undefined method `http_accept_language' for {}:Hash
     # ./app/controllers/application_controller.rb:22:in `set_locale'
     # /home/masonscott/.rvm/gems/ruby-2.3.0/gems/activesupport-4.1.4/lib/active_support/callbacks.rb:424:in `block in make_lambda'
     # /home/masonscott/.rvm/gems/ruby-2.3.0/gems/activesupport-4.1.4/lib/active_support/callbacks.rb:160:in `block in halting'
     # /home/masonscott/.rvm/gems/ruby-2.3.0/gems/activesupport-4.1.4/lib/active_support/callbacks.rb:229:in `block in halting'
     # /home/masonscott/.rvm/gems/ruby-2.3.0/gems/activesupport-4.1.4/lib/active_support/callbacks.rb:229:in `block in halting'
     # /home/masonscott/.rvm/gems/ruby-2.3.0/gems/activesupport-4.1.4/lib/active_support/callbacks.rb:166:in `block in halting'
     # /home/masonscott/.rvm/gems/ruby-2.3.0/gems/activesupport-4.1.4/lib/active_support/callbacks.rb:166:in `block in halting'
     # /home/masonscott/.rvm/gems/ruby-2.3.0/gems/activesupport-4.1.4/lib/active_support/callbacks.rb:166:in `block in halting'
     # /home/masonscott/.rvm/gems/ruby-2.3.0/gems/activesupport-4.1.4/lib/active_support/callbacks.rb:86:in `run_callbacks'
     # /home/masonscott/.rvm/gems/ruby-2.3.0/gems/actionpack-4.1.4/lib/abstract_controller/callbacks.rb:19:in `process_action'
     # /home/masonscott/.rvm/gems/ruby-2.3.0/gems/actionpack-4.1.4/lib/action_controller/metal/rescue.rb:29:in `process_action'
     # /home/masonscott/.rvm/gems/ruby-2.3.0/gems/actionpack-4.1.4/lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
     # /home/masonscott/.rvm/gems/ruby-2.3.0/gems/activesupport-4.1.4/lib/active_support/notifications.rb:159:in `block in instrument'
     # /home/masonscott/.rvm/gems/ruby-2.3.0/gems/activesupport-4.1.4/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
     # /home/masonscott/.rvm/gems/ruby-2.3.0/gems/activesupport-4.1.4/lib/active_support/notifications.rb:159:in `instrument'
     # /home/masonscott/.rvm/gems/ruby-2.3.0/gems/actionpack-4.1.4/lib/action_controller/metal/instrumentation.rb:30:in `process_action'
     # /home/masonscott/.rvm/gems/ruby-2.3.0/gems/actionpack-4.1.4/lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
     # /home/masonscott/.rvm/gems/ruby-2.3.0/gems/activerecord-4.1.4/lib/active_record/railties/controller_runtime.rb:18:in `process_action'
     # /home/masonscott/.rvm/gems/ruby-2.3.0/gems/actionpack-4.1.4/lib/abstract_controller/base.rb:136:in `process'
     # /home/masonscott/.rvm/gems/ruby-2.3.0/gems/actionview-4.1.4/lib/action_view/rendering.rb:30:in `process'
     # /home/masonscott/.rvm/gems/ruby-2.3.0/gems/actionpack-4.1.4/lib/action_controller/test_case.rb:595:in `process'
     # /home/masonscott/.rvm/gems/ruby-2.3.0/gems/actionpack-4.1.4/lib/action_controller/test_case.rb:64:in `process'
     # /home/masonscott/.rvm/gems/ruby-2.3.0/gems/actionpack-4.1.4/lib/action_controller/test_case.rb:501:in `post'
     # ./spec/controllers/reviews_controller_spec.rb:10:in `block (3 levels) in <top (required)>'

Finished in 0.01243 seconds (files took 1.96 seconds to load)
3 examples, 1 failure

Failed examples:

rspec ./spec/controllers/reviews_controller_spec.rb:6 # ReviewsController POST #create Review can be created

Coverage report generated for RSpec to /home/masonscott/Workspace/cs169/cs169-smileygo/coverage. 5 / 18 LOC (27.78%) covered.

What might be the issue here?


Solution

  • What might be the issue here?

    The issue would seem to be that you're calling ActionController::Metal#env, an @_env variable hasn't been set, and you're being returned an empty hash, which doesn't respond to the http_accept_language method, but I don't think that knowledge will solve your problem.

    It looks like you're using the HttpAcceptLanguage gem, and looking at its README file, it looks like that if you're going to use the http_accept_language method in a controller, you don't need to use it against env unless you're writing Rack middleware. Here is the example from the README file:

    class SomeController < ApplicationController
      before_filter :set_locale
    
      private
        def set_locale
          I18n.locale = http_accept_language.compatible_language_from(I18n.available_locales)
        end
    end
    

    Try changing your code to be like the example (ie removing env) and see if that works.