gpt4 book ai didi

ruby-on-rails-4 - RSpec 3.4/Rails 4.2 : how to test view containing button_to

转载 作者:行者123 更新时间:2023-12-04 07:00:11 26 4
gpt4 key购买 nike

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

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,并且 View 有一个条件,如果用户是 devcomm,则只显示 button_to。

我找到了 this related StackOverflow question ,但因为导致问题的按钮会删除记录,所以我想将其保留为按钮而不是链接,所以我不能只将其更改为 link_to。

关于如何测试包含 button_to 的 View 有什么想法吗?

最佳答案

我发现了问题。在 View 中,我没有提供要采取行动的对象。我改变了 <%= 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"
%>

关于ruby-on-rails-4 - RSpec 3.4/Rails 4.2 : how to test view containing button_to,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37149659/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com