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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With