[rspec-users] @current_user in controller specs

John Gadbois jgadbois at gmail.com
Fri Dec 10 17:15:13 EST 2010


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 @

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)

        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)

Any help would be appreciated.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20101210/d2a6bf61/attachment.html>

More information about the rspec-users mailing list