ruby on rails - RSpec and protected methods, controller spec for current_user -


i might going @ wrong way. i'm doing spec first, bdd/tdd , hit bump.

i have application_controller_spec.rb

require "spec_helper"  describe applicationcontroller   describe "current_user"     "should return nil if no 1 logged in"       subject.current_user.should be_nil     end     "should return logged in user"       hash = {user_id: "my_id"}       subject.should_receive(:session).and_return hash       subject.current_user.should == "my_id"     end   end end 

which works fine without protected keyword.

application_controller.rb

class applicationcontroller < actioncontroller::base   protect_from_forgery   helper_method :current_user    protected   def current_user     session[:user_id]   end end 

with protected enabled, error msg

nomethoderror: protected method `current_user' called #<applicationcontroller:0x2a90888> 

i should able test using helper_method... suggestions?

helper_method makes method available in views, not controller, according the docs.

if need access method controller specs, use send:

subject.send(:current_user).should be_nil 

but might want consider whether testing non-public methods makes sense, or if better test using view specs. or whether method needs protected in first place. might instructive see how devise , authlogic implement testing current_user methods.


Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -