简体   繁体   English

rspec控制器放置更新测试不起作用

[英]rspec controller put update test not working

I am stuck on a rspec test that is testing a update method in my controller. 我陷入了rspec测试中,该测试正在测试控制器中的更新方法。 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. 我已经用Google搜索并阅读了该网站上的许多帖子,但是没有一个打开我的头上的灯泡。

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: 编辑表单对字段使用name =“ project_code [...]”:即:

<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. 我正在尝试创建params [:project_code]传递给该方法。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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