RSpec3.4/Rails4.2:如何测试包含button_to的视图

RSpec 3.4/Rails 4.2: how to test view containing button_to

我正在使用 Rails 4.2 和 RSpec 3.4。我正在尝试为包含 <%= button_to ... %> 删除按钮的视图编写视图测试。我的测试失败并出现此错误:

Failures:

  1) projects/show.html.erb get secret project as nondisclosed devcomm user shows the obfuscated name and disclosed user
     Failure/Error: <%= button_to "Delete this project and reassign all children to parent", {}, method: "delete", data: { confirm: "Are you sure?" }, class: "btn btn-danger" %>

     ActionView::Template::Error:
       No route matches {:action=>"show", :controller=>"projects"}
     # ./app/views/projects/show.html.erb:13:in `_app_views_projects_show_html_erb___2105827750050220814_23458641445220'
     # ./spec/views/projects/show.html.erb_spec.rb:23:in `block (3 levels) in <top (required)>'
     # ------------------
     # --- Caused by: ---
     # ActionController::UrlGenerationError:
     #   No route matches {:action=>"show", :controller=>"projects"}
     #   ./app/views/projects/show.html.erb:13:in `_app_views_projects_show_html_erb___2105827750050220814_23458641445220'

测试如下:

require 'rails_helper'

RSpec.describe "projects/show.html.erb", type: :view do
  fixtures :users, :projects

  context "get secret project as disclosed user" do
    it "shows the unobfuscated name and disclosed user" do
      assign(:user, users(:nondevcomm1))
      assign(:project, projects(:secretproject2))

      render

      expect(rendered).to match "Secret project 2"
      expect(rendered).to match "Boo Wah, Another job title"
    end
  end

  context "get secret project as nondisclosed devcomm user" do
    it "shows the obfuscated name and disclosed user" do
      assign(:user, users(:devcomm1))
      assign(:project, projects(:secretproject2))

      render  ### This is where it fails

      expect(rendered).to match "Project 3"
      expect(rendered).to match "Boo Wah, Another job title"
    end
  end

end

第一个测试通过而第二个测试失败的原因是因为第一个测试中的用户是nondevcomm,并且视图有条件只显示button_to如果用户是devcomm。

我找到了 this related Whosebug question,但是因为导致问题的按钮删除了记录,所以我想将它保留为按钮而不是 link,所以我不能只更改它link_to.

关于如何测试包含 button_to 的视图的任何想法?

我发现了问题。在视图中,我没有提供要采取行动的对象。我将 <%= button_to ... %> 更改为此,现在我的测试通过了:

<%= button_to "Delete this project and reassign all children to parent", @project, method: "delete", data: { confirm: "Are you sure?" }, class: "btn btn-danger"
%>