Very interesting Tim, that will certainly be useful for me in the future. Thanks for sharing!
El 09/10/13 20:56, Tim Griffin escribió: > Hi all; > > I'm just throwing this back in the pot for anyone who happens to hit the > same problem with a test against a Hobo-based controller. > > In running a particular test, I was logging in as one user, doing an > operation, logging out and logging in as another user, but the > controller I was testing was holding onto the first user, and throwing > me permission errors for subsequent operations. > > Why wasn't Hobo's current_user being switched to my new user? > > I scratched my head for a long time on this one until Bryan kindly > pointed me to this sneaky method: > > * http://apidock.com/ruby/Object/instance_variable_set* > > which let me reach in and reset the protected "current_user" within my > controller. > > In my test_helper, I have these two methods: > > def controller_test_signin(user) > old_controller = @controller > @controller = UsersController.new > post :login, :login => user.email_address, :password => user.password > > # Verify that we've signed in > assert_not_nil( session[:user] ) > > @controller = old_controller > end > > > def controller_test_signout > old_controller = @controller > @controller = UsersController.new > post :logout > > # During the lifetime of a Controller Test, the controller instance > # isn't reloaded as it would be during a normal request. Because > # the test case holds onto that instance, we need to reach in and > # wipe out the controller's stored @current_user. Hobo's current_user > # method will then reload the current_user from the session. > old_controller.instance_variable_set(:@current_user, nil) # <-- Magic > > # Verify that we've signed out > assert_nil( session[:user] ) > > @controller = old_controller > end > > My test case was like this (yes, it borders heavily on an Integration test): > > def setup > super #Make sure we call the setup in test_helper > controller_test_signin(@user) > > @package = create( :package, :draft, :mortgage, > :with_draft_mortgage_documents, :owner => @user, :region => @region ) > end > > it "after rejection, rejection acknowledgement must be displayed" do > > # First, submit the package. > put :do_submit, :id => @package.id > > # Reload the package > @package = Package.find(@package.id) > > # Make sure the package was submitted > assert_equal(:submitted, @package.state.to_sym, "Package > submission failed.") > > user = session[:user] > # Switch user to Officer > controller_test_signout > controller_test_signin(@officer) > > # Make sure we're an Officer now > assert_not_equal(user, session[:user], "Session user should be > different.") > > # Put the package into review > put :do_review, :id => @package.id > flash[:error].must_be_nil > > What I had overlooked in my controller testing was that the controller > instance isn't refreshed as it would be during a normal request, so > current_user wasn't getting reset. D'oh. > > Tim > > > > -- > You received this message because you are subscribed to the Google > Groups "Hobo Users" group. > To unsubscribe from this group and stop receiving emails from it, send > an email to [email protected]. > To post to this group, send email to [email protected]. > Visit this group at http://groups.google.com/group/hobousers. > For more options, visit https://groups.google.com/groups/opt_out. -- Ignacio Huerta Arteche http://www.ihuerta.net Teléfono: 0034 645 70 77 35 Email realizado con software libre -- You received this message because you are subscribed to the Google Groups "Hobo Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/hobousers. For more options, visit https://groups.google.com/groups/opt_out.
