ruby - undefined method `authenticate' for nil:NilClass While Testing Custom OmniAuth Strategy in Rails With Devise -
i wrote custom omniauth strategy , trying write rspec tests around it, keep getting error:
nomethoderror: undefined method `authenticate' nil:nilclass # ./controllers/application_controller.rb:58:in `block in <class:applicationcontroller>' # ./lib/omniauth/custom_strategy.rb:135:in `block in callback_phase' # ./lib/omniauth/custom_strategy.rb:119:in `callback_phase' # ./spec/lib/omniauth/custom_strategy_spec.rb:62:in `block (3 levels) in <top (required)>'
after digging through devise code, found error originating in lib/devise/controllers/helpers.rb, current_user method gets defined in applicationcontroller:
def current_#{mapping} @current_#{mapping} ||= warden.authenticate(:scope => :#{mapping}) end
in case, warden defined as:
def warden request.env['warden'] end
so, in spec, since it's not controller spec, put in custom code create rack app (as seen here: https://github.com/mkdynamic/omniauth/blob/master/spec/omniauth/strategies/developer_spec.rb) so:
# outside of spec block:
rspec.configure |config| config.include rack::test::methods end
# inside spec block:
let(:app){ rack::builder.new |b| b.use rack::session::cookie b.use omniauth::strategies::customstrategy b.run lambda{|env| [200, {}, ['not found']]} end.to_app }
the problem here rack::test::methods don't include request object, rspec provides in controller tests. so, request.env not defined , therefore warden not defined.
i tried including devise::testhelpers, described here (all ruby tests raising: undefined method `authenticate' nil:nilclass), since @request not defined, throws error.
i've tried many other solutions, none seem deal non-controller tests, since rely on request object. has had experience these issues , can shed light on possible solutions?
thanks in advance.
Comments
Post a Comment