2012-01-17 10 views
0

私は@trans_op.coverages.buildラインが存在することをことをテストすることができますどのようにネストされた形関連モデルが構築されたことをテストしますか?

def edit 
    @trans_op = TransportOperator.find params[:id] 
    @trans_op.coverages.build 
end 

用コントローラで構築しています関連でモデルを持っていますか?

describe "GET edit" do 
    let!(:trans_op){ Factory :trans_op } 

    it "should be a success" do 
     get :edit, id: trans_op 
     response.should be_success 
    end 

    # this test needs to be fixed 
    it "should build an empty coverage if there are none" do 
     get :edit, id: trans_op 
     # ??? 
    end 
    end 

私はからかってみた:

it "should build an empty coverage if there are none" do 
    trans_op.coverages.should_receive(:build) 
    get :edit, id: trans_op 
end 

1) TransportOperatorsController GET edit should add an empty coverage if there are none 
    Failure/Error: Unable to find matching line from backtrace 
    ([]).build(any args) 
     expected: 1 time 
     received: 0 times 

it "should build an empty coverage if there are none" do 
    get :edit, id: trans_op 
    trans_op.coverages.count.should == 1 
end 

障害カウント:

1) TransportOperatorsController GET edit should add an empty coverage if there are none 
    Failure/Error: trans_op.coverages.count.should == 1 
    expected: 1 
      got: 0 (using ==) 

答えて

1

をあなたのスペックにassignsを介してコントローラ変数にアクセスすることができます。したがって、たとえば、これはトリックを行う可能性があります:

it "should build an empty coverage if there are none" do 
    get :edit, id: trans_op 
    assigns(:transop).should == trans_op 
    assigns(:transop).coverages.should have(1).item 
end 

http://rubydoc.info/gems/rspec-rails/2.8.1/file/README.md#assigns

を参照してくださいに
関連する問題