My controller def edit @user = User.find(get_param(:id, :integer)) @user = current_user if !is_admin? end ********************* My spec describe "GET edit" do ############################################################## should_require_login :get, :edit ############################################################## describe "authenticated user" do ############################################################## before(:each) do login_as_user @user = mock_model(User) User.stub!(:find).and_return(@user) controller.stub!(:is_admin?).and_return(false) end ############################################################## def do_get get :edit, :id => 1 end ############################################################## it "should find user and return object" do User.should_receive(:find).with(1).and_return(@user) get :edit, :id => 1 assigns[:user].should == @user end end end ************************ The error 1) ''UsersController GET edit authenticated user should find user and return object'' FAILED expected: #<User:0x10cbaec @name="User_1016">, got: #<User:0x10cf228 @name="User_1015"> (using ==) ************************************ I know I am stubbing the is_admin? method in a wrong way. Can anyone help me? Thank you EM -- View this message in context: http://www.nabble.com/Help-in-Stubing-is_admin--method-tp25965314p25965314.html Sent from the rspec-users mailing list archive at Nabble.com.
On 19 Oct 2009, at 21:55, Elza wrote:> def edit > @user = User.find(get_param(:id, :integer)) > @user = current_user if !is_admin? > endHi Elza Am I right thinking your intent here is the same as: def edit @user = if is_admin? User.find(get_param(:id, :integer)) else current_user end end If so, a hit to /users/5 would edit user 5 if you were an admin user, but would always edit the logged in user otherwise. Since you have> should_require_login :get, :editas part of the spec, that''d mean you could never be in the latter situation. Sorry if this is missing the point (I''ve had a long day, may have misread your code...), but I thought I''d better establish your intent before posting an irrelevant reply about the expectation error. Ashley -- http://www.patchspace.co.uk/ http://www.linkedin.com/in/ashleymoran http://aviewfromafar.net/
On 20 Oct 2009, at 19:31, elza.morelli at tagview.com.br wrote:> Thank you for your replay. I did not mention that I have a method > called in before_filter to set @user. > before_filter :find_current, :only => > [:show, :edit, :update, :destroy] > The method is > def find_current > @user = User.find(get_param(:id, :integer)) > end > > After this call the controller is > def edit > @user = current_user if !is_admin? > endElza, Copying this back in to the main list as it is more valuable to discuss it there. Can you please confirm that you are not confusing the concept of "logged in user" and "user to be edited with the Users controller"? That is what appears to be happening to me. Ashley -- http://www.patchspace.co.uk/ http://www.linkedin.com/in/ashleymoran http://aviewfromafar.net/