使用水豚测试表单 text_field 的存在

Flo*_*ahl 3 forms ruby-on-rails capybara

我有以下表格,我想检查文本字段是否存在。我怎样才能做到这一点 ?

<%= form_for(ownership, remote: true) do |f| %>
  <div>
    <%= f.text_field :confirm, value: nil %>
    <%= f.hidden_field :start_date, value: Time.now %>
  </div>
  <%= f.submit t('button.ownership.take.confirmation'), class: "btn btn-small"%>
<% end %>
Run Code Online (Sandbox Code Playgroud)

这是我的测试:

describe "for not confirmed ownership" do

  before do
    FactoryGirl.create(:agreed_ownership, user: current_user, product: product)
    be_signed_in_as(current_user)
    visit current_page
  end

  # it { should_not have_text_field(confirm) }
  it { should_not have_button(t('button.ownership.take.confirmation')) }
end
Run Code Online (Sandbox Code Playgroud)

Dan*_*ght 5

你会使用一个has_css?期望:

it "should have the confirm input field" do
  visit current_page

  expect(page).to have_css('input[type="text"]')
end
Run Code Online (Sandbox Code Playgroud)

您也可以使用其他 jQuery 样式的选择器来过滤输入字段上的其他属性。例如,'input[type="text"][name*="confirm"]'将选择confirm出现在输入字段的name属性中。

要设置该字段存在的期望,您可以使用to_not您的期望:expect(page).to_not have_css('input[type="text"]')

奖励:这是较旧的 -should样式语法:

it "should have the confirm input field" do
  visit current_page

  page.should have_css('input[type="text"]')
end

it "shouldn't have the confirm input field" do
  visit current_page

  page.should_not have_css('input[type="text"]')
end
Run Code Online (Sandbox Code Playgroud)