1

私は RSpec を初めて使用しますが、Rails 3 アプリの新しい機能のために RSpec2 コントローラー スペックを作成しようとしています。所属するモデルが :user で、モデルの新しいインスタンスを作成するときに、コントローラーによってユーザーが確実に設定されるようにしたいと考えています。

現在ログオンしているユーザーは、作成アクションでモデルに割り当てられたユーザーである必要があります。スペックが動かなくて困っています。これが私が持っているものです

# this was generated by rspec-rails and sets up my mock model
def mock_plan_order(stubs={})
  (@mock_plan_order ||= mock_model(PlanOrder).as_null_object).tap do |plan_order|
    plan_order.stub(stubs) unless stubs.empty?
  end
end

# here's the actual test that's not working
context "user logged in" do
    before(:each) do
      login_user # this logs in and sets @current_user - this is working
    end

    describe "POST create" do
        # the spec that is not working
        it "sets the user id to the logged on user" do
          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 }
          assigns(:plan_order).user.should equal(@current_user)
        end
   end
end

これが仕様の出力です

1) PlanOrdersController user logged in POST create with valid params sets the user id to the logged on user
 Failure/Error: assigns(:plan_order).user.should equal(@current_user)

 expected #<User:58754688> => #<User:0x3808680 @name="User_1">
      got #<PlanOrder:58689360> => #<PlanOrder:0x37f8750 @name="PlanOrder_1010">

私が間違っていることを知っている人はいますか?

4

1 に答える 1

2

plan_order オブジェクトの作成をスタブ化したため、ユーザーは設定されません。モック/スタブしないでください。モックを追加して、ユーザーが設定されていることを確認してください。

于 2010-12-13T13:55:16.957 に答える