Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

capybara have_field with nil rspec expectation fails on upgrade to 2.2

After I upgraded Capybara from 2.1.0 to 2.2.1 and Poltergeist from 1.4.1 to 1.5.0, the following rspec expectation started failing in my rails app's tests:

it{ expect(page).to have_field("foo_field_id", with: nil) }

The rspec error looks like this:

 Failure/Error: it{ expect(page).to have_field("foo_field_id", with: nil) }
 Capybara::ExpectationNotMet:
   expected to find field "foo_field_id" but there were no matches. Also found "", which matched the selector but not all filters.

If I throw a breakpoint in to inspect, the value is, indeed, nil:

»  page.find_field('foo_field_id').value
=> nil

If I change "with" to "text", the assertion passes:

it{ expect(page).to have_field("foo_field_id", text: nil) }

The HTML form field looks like this:

<input class="string optional" id="foo_field_id" name="foo[field_id]" type="text">

Why is this happening?

like image 280
Lorin Hochstein Avatar asked May 27 '14 14:05

Lorin Hochstein


1 Answers

According to https://github.com/jnicklas/capybara/pull/1169, it's because of a recent change:

It's not possible to match against nil anymore because to_s gets called on what you pass to with.

The workaround is to do something like this instead:

expect(find_field("foo_field_id").text).to be_blank
like image 66
Lorin Hochstein Avatar answered Sep 30 '22 05:09

Lorin Hochstein