Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NoMethodError:undefined method `symbolize_keys' in rspec test

Here is the error for update in rspec:

  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)>'

The rspec code:

it "'update' should be successful" do

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

The update in customers controller:

  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

Any thoughts about the error? Thanks.

like image 658
user938363 Avatar asked Jan 19 '23 11:01

user938363


1 Answers

I will not enter into details on RSpec, but I just met the same error and this is how I would correct it for your code:

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

I think you can't provide your object directly to the post method, you must pass it's id as in a Hash instead.

(Please note too that this code assumes your customer exists in the test database, so your Factory must create it.)

like image 177
Romain Champourlier Avatar answered Jan 31 '23 09:01

Romain Champourlier