@current_user in controller specs

Hi,

I’m an absolute beginner with RSpec, so I apologize if this question
doesn’t
make sense.

I’m using RSpec 2, Rails 3. I have a @current_user instance variable
that I
can access in my controller. I have a controller called
PlanOrderController. In the create action of this model, I would like
to
assign the user_id of the PlanOrder object to be the user_id of the
@current_user before saving.

I have the generated rails controller specs so I’m starting with those.
Basically I want my controller spec for the create action to test to
make
sure that the newly created PlanOrder has it’s user_id property set to @
current_user.id

The code I have so far is below but it’s not working

  it "sets the user id to the logged on user" do
    @current_user = User.new(:id=>27)
    controller.stub!(:current_user).and_return(@current_user)

    PlanOrder.stub(:new).with({"days_per_week" => 3,

“description”=>“Test”, “purpose_id”=>1 }) { mock_plan_order(:save =>
true) }
post :create, :plan_order =>{“days_per_week” => 3,
“description”=>“Test”, “purpose_id”=>1 }
plan_order = assigns(:plan_order)
plan_order.user_id.should be(@current_user.id)
end

Any help would be appreciated.

Thanks,
John

You can’t say User.new(:id=>27) as ActiveRecord prevents :id from being
mass-assigned like that (in the case of an action that does
User.new(params[:user]) in the controller, people would be able to set
the
id through the POST data). Try User.new.tap {|u| u.id = 27 } instead.

– Elliot