NoMethodError:rspec测试中未定义的方法`symbolize_keys'

时间:2011-10-20 16:38:44

标签: ruby-on-rails ruby-on-rails-3.1 rspec2

以下是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)>'

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

有关错误的任何想法?感谢。

1 个答案:

答案 0 :(得分:9)

我不会详细介绍RSpec,但我遇到了同样的错误,这就是我为你的代码纠正错误的方法:

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

我认为您无法直接将您的对象提供给post方法,您必须将id传递给Hash,而不是create

(请注意,此代码假定您的客户存在于测试数据库中,因此您的工厂必须{{1}}它。)