在 Rails 的 Rspec 测试中,将 css 选择器与 dom_id 一起使用时出现错误消息 Capybara::ElementNotFound
Error message Capybara::ElementNotFound when using css selectors with dom_id in Rspec testing with Rails
我正在使用 Rspec 在 Rails 中进行测试。我试图让测试点击编辑按钮,以便它可以编辑待办事项列表。由于有多个编辑链接,我在 index.html.erb 文件中添加了一个 dom_id:
<h1>Listing Todo Lists</h1>
<table>
<thead>
<tr>
<th>Title</th>
<th>Description</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @todo_lists.each do |todo_list| %>
<tr id="<%= dom_id(todo_list) %>">
<td><%= todo_list.title %></td>
<td><%= todo_list.description %></td>
<td><%= link_to 'Show', todo_list %></td>
<td><%= link_to 'Edit', edit_todo_list_path(todo_list) %></td>
<td><%= link_to 'Destroy', todo_list, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Todo list', new_todo_list_path %>
我不断收到错误消息:
1) Editing todo lists Updates todo list with correct info
Failure/Error:
within "#todo_list_#{todo_list.id}" do
click_link "Edit"
end
Capybara::ElementNotFound:
Unable to find css "#todo_list_"
# ./spec/features/todo_lists/edit_spec.rb:11:in `update_todo_list'
# ./spec/features/todo_lists/edit_spec.rb:24:in `block (2 levels) in <top (required)>'
这是我的 edit_spec.rb 文件:
require 'rails_helper'
describe "Editing todo lists" do
def update_todo_list(options={})
options[:title] ||= "My todo list"
options[:description] ||= "This is my todo list"
todo_list = options[:todo_list]
visit "/todo_lists"
within "#todo_list_#{todo_list.id}" do
click_link "Edit"
end
fill_in "Title", with: options[:title]
fill_in "Description", with: options[:description]
click_button "Update Todo list"
end
it "Updates todo list with correct info" do
todo_list = TodoList.new(title: "Grocery list", description: "This is my grocery list")
update_todo_list(todo_list: todo_list, title: "New title", description: "New description")
todo_list.reload
expect(page).to_have content("Todo list was successfully updated")
expect(todo_list.title).to eq("New title")
expect(todo_list.description).to eq("New description")
end
end
我的控制器或模型没有问题,因为当我尝试在服务器启动时编辑待办事项列表时,它工作正常。任何帮助将不胜感激!
一旦您只在您的模型上调用 new
,它就不会被保存,因此没有 id
。这就是为什么错误说:
Unable to find css "#todo_list_"
因此您必须保存您的模型,这样它就会有一个 id
插值到选择器上。
it "Updates todo list with correct info" do
todo_list = TodoList.create!(title: "Grocery list", description: "This is my grocery list")
...
我正在使用 Rspec 在 Rails 中进行测试。我试图让测试点击编辑按钮,以便它可以编辑待办事项列表。由于有多个编辑链接,我在 index.html.erb 文件中添加了一个 dom_id:
<h1>Listing Todo Lists</h1>
<table>
<thead>
<tr>
<th>Title</th>
<th>Description</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @todo_lists.each do |todo_list| %>
<tr id="<%= dom_id(todo_list) %>">
<td><%= todo_list.title %></td>
<td><%= todo_list.description %></td>
<td><%= link_to 'Show', todo_list %></td>
<td><%= link_to 'Edit', edit_todo_list_path(todo_list) %></td>
<td><%= link_to 'Destroy', todo_list, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Todo list', new_todo_list_path %>
我不断收到错误消息:
1) Editing todo lists Updates todo list with correct info
Failure/Error:
within "#todo_list_#{todo_list.id}" do
click_link "Edit"
end
Capybara::ElementNotFound:
Unable to find css "#todo_list_"
# ./spec/features/todo_lists/edit_spec.rb:11:in `update_todo_list'
# ./spec/features/todo_lists/edit_spec.rb:24:in `block (2 levels) in <top (required)>'
这是我的 edit_spec.rb 文件:
require 'rails_helper'
describe "Editing todo lists" do
def update_todo_list(options={})
options[:title] ||= "My todo list"
options[:description] ||= "This is my todo list"
todo_list = options[:todo_list]
visit "/todo_lists"
within "#todo_list_#{todo_list.id}" do
click_link "Edit"
end
fill_in "Title", with: options[:title]
fill_in "Description", with: options[:description]
click_button "Update Todo list"
end
it "Updates todo list with correct info" do
todo_list = TodoList.new(title: "Grocery list", description: "This is my grocery list")
update_todo_list(todo_list: todo_list, title: "New title", description: "New description")
todo_list.reload
expect(page).to_have content("Todo list was successfully updated")
expect(todo_list.title).to eq("New title")
expect(todo_list.description).to eq("New description")
end
end
我的控制器或模型没有问题,因为当我尝试在服务器启动时编辑待办事项列表时,它工作正常。任何帮助将不胜感激!
一旦您只在您的模型上调用 new
,它就不会被保存,因此没有 id
。这就是为什么错误说:
Unable to find css "#todo_list_"
因此您必须保存您的模型,这样它就会有一个 id
插值到选择器上。
it "Updates todo list with correct info" do
todo_list = TodoList.create!(title: "Grocery list", description: "This is my grocery list")
...