Marcin Doliwa
Marcin Doliwa

Reputation: 3659

checking if the form fields exist with have_field

I have simple form for user (form_for @user) signup with password field:

<div class="control-group"> 
  <%= f.label :password, :class => 'control-label' %>
  <div class="controls"> 
    <%= f.password_field :password, :class => 'password_field' %>
  </div>
</div>

In my rspec/capybara test I want to check if this password field exists I try:

it {should have_field("password")}

but it doesn't work. The same with "user_password" (as this is this password field id) or "user[password]" (this is this field name)

Not sure if have_field is correct matcher to use, as capybara documentation doesn't explain what it is. WHat's the best way to test if form field exists?

Upvotes: 0

Views: 3745

Answers (3)

Andrew Kozin
Andrew Kozin

Reputation: 1022

Your code didn't work because instead:

it { page.should have_field("password") }

you should use either

specify { page.should have_field("password") }

or

it { should have_field("password") }

Upvotes: 3

Andrew Kozin
Andrew Kozin

Reputation: 1022

Here, in reply to myself I've solved the problem, explicitly using :type => :password in a spec.

Upvotes: 1

zetetic
zetetic

Reputation: 47548

Try replacing have_field with have_selector.

Upvotes: 0

Related Questions