我正在尝试测试ApplicationController的行为,但我一直遇到错误。以下是我的规格:
describe ApplicationController do
#Base class should be inferred
controller do
def not_found
raise ActiveRecord::RecordNotFound
end
end
# Routing
#----------------------------------------------------------------------------
def with_error_routing
with_routing do |map|
map.draw do
get '/not_found' => "anonymous#not_found", :as => :not_found
get '/home' => "anonymous#home", :as => :home
end
yield
end
end
describe "handling ActiveRecord::RecordNotFound" do
it "renders the 404 template", :failing => true do
with_error_routing do
@request.env['HTTPS'] = 'on'
get :not_found, :format => 'html'
response.should redirect_to(home_path)
end
end
end
end
我不断收到的错误消息是:ActionController::RoutingError: No route matches {:controller=>"anonymous"}
还有其他人遇到过这个问题吗?我觉得我的示例与测试ApplicationController的其他示例匹配。
这是回溯:
Failures:
1) ApplicationController handling ActiveRecord::RecordNotFound
Failure/Error: get :not_found
ActionController::RoutingError:
No route matches {:controller=>"anonymous", :action=>"not_found"}
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/actionpack-3.2.13/lib/action_dispatch/routing/route_set.rb:544:in `raise_routing_error'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/actionpack-3.2.13/lib/action_dispatch/routing/route_set.rb:540:in `rescue in generate'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/actionpack-3.2.13/lib/action_dispatch/routing/route_set.rb:532:in `generate'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/actionpack-3.2.13/lib/action_dispatch/routing/route_set.rb:573:in `generate'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/actionpack-3.2.13/lib/action_dispatch/routing/route_set.rb:598:in `url_for'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/actionpack-3.2.13/lib/action_dispatch/routing/url_for.rb:148:in `url_for'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/actionpack-3.2.13/lib/action_controller/metal/redirecting.rb:105:in `_compute_redirect_to_location'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/actionpack-3.2.13/lib/action_controller/metal/redirecting.rb:74:in `redirect_to'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/actionpack-3.2.13/lib/action_controller/metal/flash.rb:25:in `redirect_to'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/actionpack-3.2.13/lib/action_controller/metal/instrumentation.rb:60:in `block in redirect_to'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/activesupport-3.2.13/lib/active_support/notifications.rb:123:in `block in instrument'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/activesupport-3.2.13/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/activesupport-3.2.13/lib/active_support/notifications.rb:123:in `instrument'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/actionpack-3.2.13/lib/action_controller/metal/instrumentation.rb:59:in `redirect_to'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/gritter-1.0.3/lib/gritter/gflash.rb:8:in `redirect_to'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/actionpack-3.2.13/lib/action_controller/metal/force_ssl.rb:33:in `block in force_ssl'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:418:in `_run__1662327083837902210__process_action__2977905806469662832__callbacks'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:405:in `__run_callback'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:81:in `run_callbacks'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/actionpack-3.2.13/lib/abstract_controller/callbacks.rb:17:in `process_action'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/actionpack-3.2.13/lib/action_controller/metal/rescue.rb:29:in `process_action'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/actionpack-3.2.13/lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/activesupport-3.2.13/lib/active_support/notifications.rb:123:in `block in instrument'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/activesupport-3.2.13/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/activesupport-3.2.13/lib/active_support/notifications.rb:123:in `instrument'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/actionpack-3.2.13/lib/action_controller/metal/instrumentation.rb:29:in `process_action'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/actionpack-3.2.13/lib/action_controller/metal/params_wrapper.rb:207:in `process_action'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/activerecord-3.2.13/lib/active_record/railties/controller_runtime.rb:18:in `process_action'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/actionpack-3.2.13/lib/abstract_controller/base.rb:121:in `process'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/actionpack-3.2.13/lib/abstract_controller/rendering.rb:45:in `process'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/actionpack-3.2.13/lib/action_controller/metal/testing.rb:17:in `process_with_new_base_test'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/actionpack-3.2.13/lib/action_controller/test_case.rb:475:in `process'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/actionpack-3.2.13/lib/action_controller/test_case.rb:49:in `process'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/devise-2.2.4/lib/devise/test_helpers.rb:19:in `block in process'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/devise-2.2.4/lib/devise/test_helpers.rb:72:in `catch'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/devise-2.2.4/lib/devise/test_helpers.rb:72:in `_catch_warden'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/devise-2.2.4/lib/devise/test_helpers.rb:19:in `process'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/actionpack-3.2.13/lib/action_controller/test_case.rb:392:in `get'
# ./spec/controllers/application_controller_spec.rb:19:in `block (2 levels) in <top (required)>'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/rspec-core-2.13.1/lib/rspec/core/example.rb:114:in `instance_eval'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/rspec-core-2.13.1/lib/rspec/core/example.rb:114:in `block in run'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/rspec-rails-2.13.2/lib/rspec/rails/example/controller_example_group.rb:160:in `call'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/rspec-rails-2.13.2/lib/rspec/rails/example/controller_example_group.rb:160:in `block (2 levels) in <module:ControllerExampleGroup>'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/rspec-core-2.13.1/lib/rspec/core/extensions/instance_eval_with_args.rb:16:in `instance_exec'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/rspec-core-2.13.1/lib/rspec/core/extensions/instance_eval_with_args.rb:16:in `instance_eval_with_args'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/rspec-core-2.13.1/lib/rspec/core/example.rb:247:in `instance_eval_with_args'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/rspec-core-2.13.1/lib/rspec/core/hooks.rb:87:in `block (2 levels) in run'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/rspec-core-2.13.1/lib/rspec/core/hooks.rb:85:in `call'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/rspec-core-2.13.1/lib/rspec/core/hooks.rb:85:in `run'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/rspec-core-2.13.1/lib/rspec/core/hooks.rb:418:in `run_hook'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:329:in `run_around_each_hooks'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/rspec-core-2.13.1/lib/rspec/core/example.rb:256:in `with_around_each_hooks'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/rspec-core-2.13.1/lib/rspec/core/example.rb:111:in `run'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:390:in `block in run_examples'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:386:in `map'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:386:in `run_examples'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:371:in `run'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/rspec-core-2.13.1/lib/rspec/core/command_line.rb:28:in `block (2 levels) in run'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/rspec-core-2.13.1/lib/rspec/core/command_line.rb:28:in `map'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/rspec-core-2.13.1/lib/rspec/core/command_line.rb:28:in `block in run'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/rspec-core-2.13.1/lib/rspec/core/reporter.rb:34:in `report'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/rspec-core-2.13.1/lib/rspec/core/command_line.rb:25:in `run'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/rspec-core-2.13.1/lib/rspec/core/runner.rb:80:in `run'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/spring-0.0.8/lib/spring/commands.rb:110:in `call'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/spring-0.0.8/lib/spring/application.rb:71:in `block in serve'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/spring-0.0.8/lib/spring/application.rb:66:in `fork'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/spring-0.0.8/lib/spring/application.rb:66:in `serve'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/spring-0.0.8/lib/spring/application.rb:49:in `block in run'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/spring-0.0.8/lib/spring/application.rb:43:in `loop'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/spring-0.0.8/lib/spring/application.rb:43:in `run'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/spring-0.0.8/lib/spring/application.rb:37:in `start'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/spring-0.0.8/lib/spring/application_manager.rb:95:in `block in start_child'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/spring-0.0.8/lib/spring/application_manager.rb:82:in `fork'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/spring-0.0.8/lib/spring/application_manager.rb:82:in `start_child'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/spring-0.0.8/lib/spring/application_manager.rb:27:in `start'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/spring-0.0.8/lib/spring/application_manager.rb:54:in `block in with_child'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/spring-0.0.8/lib/spring/application_manager.rb:21:in `synchronize'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/spring-0.0.8/lib/spring/application_manager.rb:43:in `with_child'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/spring-0.0.8/lib/spring/application_manager.rb:62:in `run'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/spring-0.0.8/lib/spring/server.rb:47:in `serve'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/spring-0.0.8/lib/spring/server.rb:35:in `block in boot'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/spring-0.0.8/lib/spring/server.rb:35:in `loop'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/spring-0.0.8/lib/spring/server.rb:35:in `boot'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/spring-0.0.8/lib/spring/server.rb:15:in `boot'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/spring-0.0.8/lib/spring/client/start.rb:13:in `call'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/spring-0.0.8/lib/spring/client/command.rb:7:in `call'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/spring-0.0.8/lib/spring/client.rb:23:in `run'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/gems/spring-0.0.8/bin/spring:4:in `<top (required)>'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/bin/spring:19:in `load'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/bin/spring:19:in `<main>'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/bin/ruby_noexec_wrapper:14:in `eval'
# /home/bryce/.rvm/gems/ruby-1.9.3-p429/bin/ruby_noexec_wrapper:14:in `<main>'
Finished in 0.54729 seconds
1 example, 1 failure
添加ApplicationController类:
class ApplicationController < ActionController::Base
force_ssl
check_authorization :unless => :devise_controller?
respond_to :html
respond_to :js
respond_to :json, :xml, :except => [:edit, :update]
respond_to :atom, :csv, :rss, :only => :index
rescue_from ActiveRecord::RecordNotFound, :with => :respond_to_not_found
rescue_from CanCan::AccessDenied, :with => :respond_to_access_denied
# SECURITY - Protect Form IDs from forgery
#----------------------------------------------------------------------------
protect_from_forgery
# Helpers
helper_method :current_user
def current_user
begin
return current_customer unless current_customer.nil?
return current_store unless current_store.nil?
rescue
return nil
end
end
private
def current_ability
Ability.new(current_user)
end
#----------------------------------------------------------------------------
def respond_to_not_found
FlashSetter.new.set(flash, nil, nil, {:flash_key => :alert, :message_key => :msg_not_found})
respond_to do |format|
format.html { redirect_to user_home_path }
end
end
#----------------------------------------------------------------------------
def respond_to_access_denied
FlashSetter.new.set(flash, nil, nil, {:flash_key => :alert, :message_key => :msg_not_authorized})
respond_to do |format|
format.html { redirect_to home_path }
end
end
end
答案 0 :(得分:2)
with_routing
块似乎与使用普通routes.draw
匿名控制器的方式略有不同。以下将起作用:
describe ApplicationController do
controller do
def not_found
raise ActiveRecord::RecordNotFound
end
end
it "handling ActiveRecord::RecordNotFound" do
routes.draw { get "not_found" => "anonymous#not_found" }
request.env['HTTPS'] = 'on'
get :not_found
response.should redirect_to(home_path)
end
end
编辑:引用拼写错误,强制使用SSL