3

次のフォームがあり、テキスト フィールドが存在するかどうかを確認したい。どうやってやるの ?

<%= 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 %>

ここで私のテスト:

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
4

1 に答える 1

5

あなたはhas_css?期待を使用します:

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

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

追加の jQuery スタイルのセレクターを使用して、入力フィールドの他の属性をフィルタリングすることもできます。たとえば、入力フィールドの属性に表示される'input[type="text"][name*="confirm"]'ように選択します。confirmname

フィールド存在しないという期待を設定するには、期待に対して次のように使用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
于 2013-07-26T12:02:53.907 に答える