我尝试在窗体的各个部分设置id,以及将id封装到ID为search
的div中。结果总是一样的。水豚找不到字段“搜索”
规格/功能/ 02_post_spec.rb
scenario 'search for post title' do
fill_post_form(post3)
fill_in "search", with: post3.title
click_button("Search")
expect(page).to_not have_content(post1.title)
expect(page).to_not have_content(post2.title)
expect(page).to have_content(post3.title)
end
规格/ spec_helper.rb
def fill_post_form(post)
visit new_post_path
fill_in 'Title', with: post.title
fill_in 'Body', with: post.body
click_button 'Create Post'
end
这将redirect_to post_path(post)
的意见/职位/ index.html.erb
<%= form_tag(posts_path, method: :get, class: "block") do %>
<div class="form-group">
<div class="input-group">
<%= text_field_tag :search, params[:search], class: "form-control", id: "search" %>
<span class="input-group-btn">
<%= submit_tag "Search", name: nil, class: "btn btn-default" %>
</span>
</div>
</div>
<% end %>
水豚输出
Failure/Error: fill_in "search", with: post3.title
Capybara::ElementNotFound:
Unable to find field "search"
查看页面上表单的原始html ...很可能他的字段被称为有点不同的东西。如果您尝试基于ID,那么您可能还需要'fill_in'#search''或类似的东西。 –
@TarynEast'fill_in'按输入元素的名称,id或相关标签文本进行搜索 - 它不接受CSS选择器,因此它将是'fill_in'search',...'不是#search –