由于I18n.locale错误,控制器HTTP方法无法使用rspec?

时间:2016-03-18 01:03:11

标签: ruby-on-rails ruby rspec internationalization rails-i18n

所以我试图用rspec测试来测试控制器方法,但由于I18n的一些问题,它们无法正常工作?我收到此错误消息:

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.

这可能是什么问题?

1 个答案:

答案 0 :(得分:1)

  

这可能是什么问题?

问题似乎是您正在调用ActionController::Metal#env,尚未设置@_env变量,并且您将返回一个空哈希,它不会响应http_accept_language方法,但我不认为知识会解决您的问题。

看起来您正在使用HttpAcceptLanguage gem并查看its README file,如果您打算在控制器中使用http_accept_language方法,那么除非你正在编写Rack中间件,否则不需要对env使用它。这是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

尝试将代码更改为示例(即删除env),看看是否有效。