2011-10-20 17 views
4

ここでは、RSpecの内の更新のための誤差である:NoMethodError:RSpecのテストで未定義のメソッド `symbolize_keys'

4) CustomersController GET customer page 'update' should be successful 
    Failure/Error: post 'update', customer 
    NoMethodError: 
     undefined method `symbolize_keys' for "1":String 
    # ./spec/controllers/customers_controller_spec.rb:38:in `block (3 levels) in <top (required)>' 

RSpecのコード:

it "'update' should be successful" do 

    customer = Factory(:customer)  
    post 'update', customer 
    response.should be_success 
end 

顧客コントローラで更新:

def update 
    @customer = Customer.find(params[:id]) 
    if @customer.update_attributes(params[:customer], :as => :roles_new_update) 
     if @customer.changed 
     @message = 'The following info have been changed\n' + @customer.changes.to_s 
     @subject ='Customer info was changed BY' + session[:user_name] 
     notify_all_in_sales_eng(@message,@subject) 
     end 

     redirect_to session[('page'+session[:page_step].to_s).to_sym], :notice => 'Customer was updated successfaully!' 
    else 
     render 'edit', :notice => 'Customer was not updated!' 
    end 
    end 

エラーについてのご意見はありますか?ありがとう。

答えて

9

私はRSpecの方法の詳細に入ることはありませんが、私はちょうど同じエラーに会って、これは私があなたのコードのためにそれを修正する方法を次のとおりです。私はあなたが直接あなたのオブジェクトを提供することができないと思います

it "'update' should be successful" do 
    customer = Factory(:customer)  
    post 'update', :id => customer.id 
    response.should be_success 
end 

postメソッドの代わりにHashのようにidを渡す必要があります。

(このコードはので、あなたの工場はcreateそれなければならない、あなたの顧客は、テスト用データベースに存在を前提としすぎに注意してください。)

+0

はおそらく、例えば、掲示顧客はあまりにもparamsは追加したいです'post' update '、:id => customer.id、:customer => {:some_attribute => "some_value"} ' – zetetic

+0

これは、' post'メソッドの2番目の引数がハッシュでなければならないことを確認できます。 – webdevguy

関連する問題