简体   繁体   中英

rspec controller put update test not working

I am stuck on a rspec test that is testing a update method in my controller. I am getting a routing error. Hoping someone smarter than I can help. I have googled and read many post on this site, but none have turned on the light-bulb in my head.

Note: this works when finding and updating a record via the browser. Just cannot figure out the test for it.

My Route for this controller update method:

PUT    /admins/project_codes/:id(.:format)          admins/project_codes#update

My controller update method:

def update
  @project_code = ProjectCode.find(params[:project_code][:id])
  if @project_code.update_attributes(params[:project_code])
    redirect_to(:action => :show, :id => params[:project_code][:id])
  else
    redirect_to(:action => :edit, :id => params[:project_code][:id], :notice => "Your record for #{params[:project_code][:code]} could not be updated at this time.")
  end
end

The edit form is using name="project_code[...]" for fields: ie:

<input id="project_code_code" name="project_code[code]" size="10" type="text" value="F-UZBEKIST" /

So, in my test, I need create a record, then update a field and pass it to my update method. I'm trying to create the params[:project_code] to pass to the method.

spec:

describe "PUT #update" do
  it "updates with valid name change" do
    code = FactoryGirl.create(:project_code)
    code.name = "new-name"
    put :update, :project_code => code.attributes
    ...
  end
end

My error:

1) Admins::ProjectCodesController PUT #update updates with valid name change
 Failure/Error: put 'update', :project_code => code.attributes
 ActionController::RoutingError:
   No route matches {:project_code=>{"id"=>"10375", "code"=>"ABCDEFG", "name"=>"new-name", "update_by"=>"jdc44", "created_at"=>"2015-10-07 13:22:31 -0400", "updated_at"=>"2015-10-07 13:22:31 -0400"}, :controller=>"admins/project_codes", :action=>"update"}

Any help would be appreciated.

看来您的PUT /admins/project_codes/:id(.:format)在url中需要一个:id参数,请尝试发送以下内容:

put :update, :id => code.id, :project_code => code.attributes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM