3

重要な更新

application_controller に次の行があります。

authorize_resource if: :html_auth

それを削除すると、すべてがうまくいくようです。authorize_resourcesテストを中断せずにすべてのコントローラーを呼び出す方法はありますか?

元の質問

匿名コントローラーを使用してRSpecでアプリケーションコントローラーをテストしています(これが標準的な方法かどうかはわかりませんが、これがGoogleが私を導いた方法です)。

これは、失敗している 1 つのテストのみを含む完全な RSpec です。

require "spec_helper"


shared_examples_for "normal errors for html format" do |action, expected_error|
  it "raises the error" do
    expect { get action}.to raise_error(expected_error)
  end
end

describe ApplicationController do
  controller do
    def runtime_error
      raise RuntimeError
    end

    def handable_json_error
      raise ApplicationController::InvalidParams, "model"
    end
  end

  before do
    @routes.draw do
      get 'anonymous/runtime_error'
      get 'anonymous/handable_json_error'
    end
  end

  describe "#handle_exceptions" do
    context "when runtime error happens" do
      describe "with http format" do
        it_behaves_like "normal errors for html format", :runtime_error, RuntimeError
      end
    end
  end
end

そして私のスペックヘルパー:

# This file is copied to spec/ when you run 'rails generate rspec:install'
require 'simplecov'
SimpleCov.start 'rails'

ENV["RAILS_ENV"] ||= 'test'

require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'authlogic/test_case'

include Authlogic::TestCase

Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }

RSpec.configure do |config|
  config.include FactoryGirl::Syntax::Methods

  config.fixture_path = "#{::Rails.root}/spec/fixtures"
  config.use_transactional_fixtures = true

  config.infer_base_class_for_anonymous_controllers = true

  config.order = "random"
end

バックトレースがコントローラーに当たらないことに気付いたので、関係があるかどうかはわかりません。完全なトレースを取得するため
の RSpec の出力:-b

rspec -b spec/controllers/application_controller_spec.rb 
F

Failures:

  1) ApplicationController#handle_exceptions when runtime error happens with http format behaves like normal errors for html format raises the error
     Failure/Error: expect { get action}.to raise_error(expected_error)
       expected RuntimeError, got #<NameError: uninitialized constant Anonymou> with backtrace:
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-3.2.13/lib/active_support/inflector/methods.rb:230:in `block in constantize'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-3.2.13/lib/active_support/inflector/methods.rb:229:in `each'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-3.2.13/lib/active_support/inflector/methods.rb:229:in `constantize'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-3.2.13/lib/active_support/core_ext/string/inflections.rb:54:in `constantize'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/cancan-1.6.10/lib/cancan/controller_resource.rb:147:in `resource_class'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/cancan-1.6.10/lib/cancan/controller_resource.rb:154:in `resource_class_with_parent'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/cancan-1.6.10/lib/cancan/controller_resource.rb:41:in `authorize_resource'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/cancan-1.6.10/lib/cancan/controller_resource.rb:10:in `block in add_before_filter'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:429:in `_run__1066669752__process_action__49729610__callbacks'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:405:in `__run_callback'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:81:in `run_callbacks'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-3.2.13/lib/abstract_controller/callbacks.rb:17:in `process_action'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-3.2.13/lib/action_controller/metal/rescue.rb:29:in `process_action'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-3.2.13/lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-3.2.13/lib/active_support/notifications.rb:123:in `block in instrument'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-3.2.13/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-3.2.13/lib/active_support/notifications.rb:123:in `instrument'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-3.2.13/lib/action_controller/metal/instrumentation.rb:29:in `process_action'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-3.2.13/lib/action_controller/metal/params_wrapper.rb:207:in `process_action'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-3.2.13/lib/active_record/railties/controller_runtime.rb:18:in `process_action'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-3.2.13/lib/abstract_controller/base.rb:121:in `process'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-3.2.13/lib/abstract_controller/rendering.rb:45:in `process'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-3.2.13/lib/action_controller/metal/testing.rb:17:in `process_with_new_base_test'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-3.2.13/lib/action_controller/test_case.rb:475:in `process'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-3.2.13/lib/action_controller/test_case.rb:49:in `process'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-3.2.13/lib/action_controller/test_case.rb:392:in `get'
         # ./spec/controllers/application_controller_spec.rb:6:in `block (3 levels) in <top (required)>'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-expectations-2.13.0/lib/rspec/matchers/built_in/raise_error.rb:22:in `call'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-expectations-2.13.0/lib/rspec/matchers/built_in/raise_error.rb:22:in `matches?'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-expectations-2.13.0/lib/rspec/expectations/handler.rb:23:in `handle_matcher'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-expectations-2.13.0/lib/rspec/expectations/expectation_target.rb:34:in `to'
         # ./spec/controllers/application_controller_spec.rb:6:in `block (2 levels) in <top (required)>'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example.rb:114:in `instance_eval'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example.rb:114:in `block in run'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/extensions/instance_eval_with_args.rb:16:in `instance_exec'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/extensions/instance_eval_with_args.rb:16:in `instance_eval_with_args'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example.rb:247:in `instance_eval_with_args'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/hooks.rb:87:in `block (2 levels) in run'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/hooks.rb:89:in `call'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/hooks.rb:89:in `run'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/hooks.rb:418:in `run_hook'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:329:in `run_around_each_hooks'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example.rb:256:in `with_around_each_hooks'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example.rb:111:in `run'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:390:in `block in run_examples'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:386:in `map'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:386:in `run_examples'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:371:in `run'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:372:in `block in run'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:372:in `map'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:372:in `run'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:372:in `block in run'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:372:in `map'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:372:in `run'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:372:in `block in run'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:372:in `map'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:372:in `run'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:372:in `block in run'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:372:in `map'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:372:in `run'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/command_line.rb:28:in `block (2 levels) in run'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/command_line.rb:28:in `map'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/command_line.rb:28:in `block in run'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/reporter.rb:34:in `report'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/command_line.rb:25:in `run'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/runner.rb:80:in `run'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/runner.rb:17:in `block in autorun'
     Shared Example Group: "normal errors for html format" called from ./spec/controllers/application_controller_spec.rb:31
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-expectations-2.13.0/lib/rspec/expectations/fail_with.rb:32:in `fail_with'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-expectations-2.13.0/lib/rspec/expectations/handler.rb:33:in `handle_matcher'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-expectations-2.13.0/lib/rspec/expectations/expectation_target.rb:34:in `to'
     # ./spec/controllers/application_controller_spec.rb:6:in `block (2 levels) in <top (required)>'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example.rb:114:in `instance_eval'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example.rb:114:in `block in run'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/extensions/instance_eval_with_args.rb:16:in `instance_exec'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/extensions/instance_eval_with_args.rb:16:in `instance_eval_with_args'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example.rb:247:in `instance_eval_with_args'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/hooks.rb:87:in `block (2 levels) in run'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/hooks.rb:89:in `call'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/hooks.rb:89:in `run'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/hooks.rb:418:in `run_hook'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:329:in `run_around_each_hooks'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example.rb:256:in `with_around_each_hooks'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example.rb:111:in `run'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:390:in `block in run_examples'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:386:in `map'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:386:in `run_examples'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:371:in `run'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:372:in `block in run'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:372:in `map'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:372:in `run'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:372:in `block in run'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:372:in `map'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:372:in `run'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:372:in `block in run'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:372:in `map'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:372:in `run'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:372:in `block in run'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:372:in `map'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:372:in `run'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/command_line.rb:28:in `block (2 levels) in run'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/command_line.rb:28:in `map'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/command_line.rb:28:in `block in run'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/reporter.rb:34:in `report'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/command_line.rb:25:in `run'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/runner.rb:80:in `run'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/runner.rb:17:in `block in autorun'

Finished in 0.052 seconds
1 example, 1 failure

Failed examples:

rspec ./spec/controllers/application_controller_spec.rb:5 # ApplicationController#handle_exceptions when runtime error happens with http format behaves like normal errors for html format raises the error

Randomized with seed 2493

Coverage report generated for RSpec to /home/fotanus/work/translation_proxy/coverage. 236 / 548 LOC (43.07%) covered.

Rails 3.2.13 と rspec 2.13.1 を使用しています

私はどこにも匿名を書いていません。次の出力を参照してください。

grep -rin --exclude-dir=log --binary-files=text Anonymou .
./tmp/cache/assets/DE8/730/sprockets%2Fa51ac8eb9ee0a56c6c04ae1c21184baf:612:            // We use an anonymous function so that context is window
./tmp/cache/assets/DE8/730/sprockets%2Fa51ac8eb9ee0a56c6c04ae1c21184baf:9800:   // understands anonymous AMD modules. A named AMD is safest and most robust
./tmp/cache/assets/C8A/4B0/sprockets%2Ff16dfcb3e634f5416377592c25469801:612:            // We use an anonymous function so that context is window
./tmp/cache/assets/C8A/4B0/sprockets%2Ff16dfcb3e634f5416377592c25469801:9800:   // understands anonymous AMD modules. A named AMD is safest and most robust
./tmp/cache/assets/D78/320/sprockets%2F95003f96fcb4963ddf27265db635eabe:612:            // We use an anonymous function so that context is window
./tmp/cache/assets/D78/320/sprockets%2F95003f96fcb4963ddf27265db635eabe:9800:   // understands anonymous AMD modules. A named AMD is safest and most robust
./tmp/cache/assets/CD9/940/sprockets%2F1b1af54f9943c03a292c5d52227619dd:612:            // We use an anonymous function so that context is window
./tmp/cache/assets/CD9/940/sprockets%2F1b1af54f9943c03a292c5d52227619dd:9800:   // understands anonymous AMD modules. A named AMD is safest and most robust
./tmp/cache/assets/DDC/400/sprockets%2Fcffd775d018f68ce5dba1ee0d951a994:612:            // We use an anonymous function so that context is window
./tmp/cache/assets/DDC/400/sprockets%2Fcffd775d018f68ce5dba1ee0d951a994:9800:   // understands anonymous AMD modules. A named AMD is safest and most robust
./spec/spec_helper.rb:22:  config.infer_base_class_for_anonymous_controllers = true
./spec/controllers/application_controller_spec.rb:23:      get 'anonymous/runtime_error'
./spec/controllers/application_controller_spec.rb:24:      get 'anonymous/handable_json_error'

なにが問題ですか?

4

3 に答える 3

4

「s」で終わる「anonymous」を活用しようとすると、Rails が混乱すると思います。これを に追加config/initializers/inflections.rb:

ActiveSupport::Inflector.inflections do |inflect|
  inflect.singular('anonymous', 'anonymous')
end

docsから、 Rails コンソールで確認できます。

"anonymous".singularize # => "anonymou"
# after adding above to inflections initializer
"anonymous".singularize # => "anonymous"
于 2013-08-14T20:32:33.363 に答える
2

これは、rspec 匿名コントローラーが CanCan を呼び出したときに発生しますload_and_authorize_resource(おそらく によって呼び出されるためApplicationController)。

CanCan は、コントローラーの名前からモデルの名前を推測します。@AbeVoelker によって言及された語形変化の問題が原因であるAnonymouと推測されます。AnonymousController

skip_load_and_authorize_resourceエラーを修正するために呼び出します。

controller do
  skip_load_and_authorize_resource

  def index
    raise RuntimeError
  end
end
于 2013-10-27T18:21:32.327 に答える