2010-12-13 2 views
1

RSpecを初めて使用していますが、Rails 3アプリケーションのいくつかの新機能用にRSpec2コントローラ仕様を作成しようとしています。私はbelongs_toというモデルを持っています:私はモデルの新しいインスタンスを作成するときに、ユーザーが私のコントローラによって設定されていることを確認したいと思います。モデルインスタンス変数のチェック方法は、RSpecコントローラの仕様で割り当てられています

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

# 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"> 

誰もが私が間違ってやっているか知っているのです持っている何ですか?

答えて

2

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

関連する問題