重要更新
我的 application_controller 中有以下行:
authorize_resource if: :html_auth
删除它似乎一切正常。有没有办法在authorize_resources
不破坏测试的情况下调用我所有的控制器?
原始问题
我正在使用匿名控制器使用 RSpec 测试我的应用程序控制器(我不确定这是标准做法,但这是 Google 引导我去做的)。
这是完整的 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
我没有在任何地方写过 Anonymou。请参阅以下输出:
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'
怎么了?