我尝试在表单的各个部分设置id,并将表单包装在id设置为search
的div中。结果总是一样的。
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
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)
<%= 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"
答案 0 :(得分:2)
您正在使用的命令fill_post_form
应该有效,假设您在页面上呈现显示的部分,并且该部分的输出实际上在页面上可见(不通过CSS隐藏)。您的方案没有显示访问实际页面,并且您没有显示fill_post_form(post3) # already in your tests
sleep 2 # wait a few seconds to make sure above method has completed whatever actions it does
puts page.html # output the current page and take a look to see if your 'search' element is actually on the page
正在执行的操作,因此很难确切地知道出现了什么问题。第一步是做一些像
x