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