rspec控制器使更新测试无法正常工作

时间:2015-10-07 17:32:23

标签: ruby-on-rails rspec-rails

我陷入了测试控制器中更新方法的rspec测试。我收到路由错误。希望有人比我能帮助更聪明。我在这个网站上用Google搜索并阅读了很多帖子,但没有人打开我头上的灯泡。

注意:这在通过浏览器查找和更新记录时有效。只是无法弄清楚它的测试。

此路由器的此控制器更新方法:

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

我的控制器更新方法:

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

编辑表单使用name =“project_code [...]”表示字段:即:

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

因此,在我的测试中,我需要创建一个记录,然后更新一个字段并将其传递给我的更新方法。我正在尝试创建params [:project_code]以传递给方法。

规格:

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

我的错误:

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"}

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

您的PUT /admins/project_codes/:id(.:format)似乎在网址中需要:id参数,请尝试发送以下内容:

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