在我的ruby on rails项目中,我的ApplicationController在下面:
class ApplicationController < ActionController::Base
def set_account
if current_user&.admin
@account = Account.find_by(id: params[:account_id]) || current_user.account
else
@account = current_user&.account
end
unless @account
redirect_to dashboard_path, notice: I18n.t('no_account_assigned_notice')
end
end
end
我正在编写controller_test以测试set_account方法。我的用户登录使用devise完成。
对于current_user
不是管理员且没有帐户的情况,我的测试是:
context 'When signed in as a general user without an account assigned' do
before do
general_user_no_account= FactoryGirl.create(:user, admin:false, account: nil)
sign_in general_user_no_account
end
it 'redirects to dashboard path with no account assigned notice' do
controller.set_account
expect(subject).to redirect_to(dashboard_path)
end
end
但是,出现以下错误:
ApplicationController set_account method When signed in as a general user without an account assigned redirects to dashboard path with no account assigned notice
Failure/Error: redirect_to dashboard_path, notice: I18n.t('no_account_assigned_notice')
Module::DelegationError:
ActionController::Metal#status= delegated to @_response.status=, but @_response is nil: #<ApplicationController:0x00000000043c3a80 @_action_has_layout=true, @_routes=nil, @_request=#<ActionController::TestRequest:0x00000000043ae720 @env={"rack.version"=>[1, 3], "rack.input"=>#<StringIO:0x00000000042d56c8>, "rack.errors"=>#<StringIO:0x00000000042d6c58>, "rack.multithread"=>true, "rack.multiprocess"=>true, "rack.run_once"=>false, "REQUEST_METHOD"=>"GET", "SERVER_NAME"=>"example.org", "SERVER_PORT"=>"80", "QUERY_STRING"=>"", "rack.url_scheme"=>"http", "HTTPS"=>"off", "SCRIPT_NAME"=>"", "CONTENT_LENGTH"=>"0", "HTTP_HOST"=>"test.host", "REMOTE_ADDR"=>"0.0.0.0", "HTTP_USER_AGENT"=>"Rails Testing", "action_dispatch.parameter_filter"=>[:password], "action_dispatch.redirect_filter"=>[], "action_dispatch.secret_token"=>nil, "action_dispatch.secret_key_base"=>"2ab3fc157e0e5a7b218fd3dc8e77e8a41a0c69a15935e6701bc2c292ead510208d29695abf784bdce95a4bbb0cbc388847a5f54bcc6359cd7a4ece3780db7a79", "action_dispatch.show_exceptions"=>false, "action_dispatch.show_detailed_exceptions"=>true, "action_dispatch.logger"=>#<ActiveSupport::Logger:0x000000000443ea50 @level=0, @progname=nil, @default_formatter=#<Logger::Formatter:0x000000000443e9b0 @datetime_format=nil>, @formatter=#<ActiveSupport::Logger::SimpleFormatter:0x00000000045e80b8 @datetime_format=nil, @thread_key="activesupport_tagged_logging_tags:36651100">, @logdev=#<Logger::LogDevice:0x000000000443e960 @shift_period_suffix=nil, @shift_size=nil, @shift_age=nil, @filename=nil, @dev=#<File:/vagrant/dispatch/log/test.log>, @mon_owner=nil, @mon_count=0, @mon_mutex=#<Thread::Mutex:0x000000000443e8c0>>, @local_levels=#<Concurrent::Map:0x0000000443e820 entries=0 default_proc=nil>>, "action_dispatch.backtrace_cleaner"=>#<Rails::BacktraceCleaner:0x00000000043b0908 @filters=[#<Proc:0x00000000043b06b0@/home/vagrant/.rvm/gems/ruby-2.4.4@dispatch/gems/railties-5.1.2/lib/rails/backtrace_cleaner.rb:14>, #<Proc:0x00000000043b0688@/home/vagrant/.rvm/gems/ruby-2.4.4@dispatch/gems/railties-5.1.2/lib/rails/backtrace_cleaner.rb:15>, #<Proc:0x00000000043b0660@/home/vagrant/.rvm/gems/ruby-2.4.4@dispatch/gems/railties-5.1.2/lib/rails/backtrace_cleaner.rb:16>, #<Proc:0x00000000043afad0@/home/vagrant/.rvm/gems/ruby-2.4.4@dispatch/gems/railties-5.1.2/lib/rails/backtrace_cleaner.rb:29>], @silencers=[#<Proc:0x00000000043afaa8@/home/vagrant/.rvm/gems/ruby-2.4.4@dispatch/gems/railties-5.1.2/lib/rails/backtrace_cleaner.rb:19>], @root="/vagrant/dispatch/">, "action_dispatch.key_generator"=>#<ActiveSupport::CachingKeyGenerator:0x00000000043b3568 @key_generator=#<ActiveSupport::KeyGenerator:0x00000000043b3608 @secret="2ab3fc157e0e5a7b218fd3dc8e77e8a41a0c69a15935e6701bc2c292ead510208d29695abf784bdce95a4bbb0cbc388847a5f54bcc6359cd7a4ece3780db7a79", @iterations=1000>, @cache_keys=#<Concurrent::Map:0x000000043b3540 entries=1 default_proc=nil>>, "action_dispatch.http_auth_salt"=>"http authentication", "action_dispatch.signed_cookie_salt"=>"signed cookie", "action_dispatch.encrypted_cookie_salt"=>"encrypted cookie", "action_dispatch.encrypted_signed_cookie_salt"=>"signed encrypted cookie", "action_dispatch.cookies_serializer"=>:json, "action_dispatch.cookies_digest"=>nil, "rack.request.cookie_hash"=>{}, "rack.session"=>{"warden.user.user.key"=>[["a02e2162-5ad9-4d43-89b8-2f03943409bf"], "$2a$04$HU2KgyE6y//kfV.Pw.a24u"]}, "rack.session.options"=>{:key=>"rack.session", :path=>"/", :domain=>nil, :expire_after=>nil, :secure=>false, :httponly=>true, :defer=>false, :renew=>false, :sidbits=>128, :cookie_only=>true, :secure_random=>SecureRandom}, "action_controller.instance"=>#<ApplicationController:0x00000000043c3a80 ...>, "warden"=>Warden::Proxy:35479380 @config={:default_scope=>:user, :scope_defaults=>{}, :default_strategies=>{:user=>[:rememberable, :database_authenticatable]}, :intercept_401=>false, :failure_app=>#<Devise::Delegator:0x0000000003ed9060>}, "action_dispatch.request.path_parameters"=>{}, "action_dispatch.request.flash_hash"=>#<ActionDispatch::Flash::FlashHash:0x0000000000ea5dd0 @discard=#<Set: {}>, @flashes={"notice"=>"No account has been assigned to you yet."}, @now=nil>}, @filtered_parameters=nil, @filtered_env=nil, @filtered_path=nil, @protocol="http://", @port=80, @method=nil, @request_method=nil, @remote_ip=nil, @original_fullpath=nil, @fullpath=nil, @ip=nil, @controller_class=ApplicationController, @custom_param_parsers={:xml=>#<Proc:0x00000000043ae540@/home/vagrant/.rvm/gems/ruby-2.4.4@dispatch/gems/actionpack-5.1.2/lib/action_controller/test_case.rb:58 (lambda)>}>, @_response=nil, @_params=<ActionController::Parameters {} permitted: false>, @current_user=#<User id: "a02e2162-5ad9-4d43-89b8-2f03943409bf", email: "edna@langosh.org", created_at: "2018-08-06 15:30:33", updated_at: "2018-08-06 15:30:33", account_id: nil, admin: false>, @account=nil, @_url_options={:host=>"test.host", :port=>nil, :protocol=>"http://", :_recall=>{}}>
我咨询过RunTimeError: ActionController::RackDelegation in rspec 2.10.1 for rails 3.1.4 application controller并提出了get :index
,get :'/'
,但还是没有运气。
感谢您的帮助。
答案 0 :(得分:0)
在这种情况下,您可以使用匿名控制器:
describe ApplicationController do
controller do
before_filter :set_account
def index
head :ok
end
end
context 'When signed in as a general user without an account assigned' do
before do
general_user_no_account= FactoryGirl.create(:user, admin:false, account: nil)
sign_in general_user_no_account
end
it 'redirects to dashboard path with no account assigned notice' do
get :index
expect(response).to redirect_to(dashboard_path)
end
end
end