Content
View differences
Updated by Christophe Bliard over 3 years ago
This code
```text
expect(page).to have_selector("input") do |input|
input.value == "hello world"
end
```
is actually evaluated like
```text
expect(page).to(have_selector("input")) do
input.value == "hello world"
end
```
Meaning the block is an argument to the `to` method. Looking into rspec source code, the block is later discarded. So the value is never actually checked.
I fixed it in a Pull Request to Capybara [https://github.com/teamcapybara/capybara/pull/2617](https://github.com/teamcapybara/capybara/pull/2617)
In the meantime, we have to deal with it
Options:
* Using `{}` makes the code work as expected, but one-line readability can suffer.
* Using `within` and `have_field(with: "hello world")` can work too
* Like in [4c03e96b24](https://github.com/opf/openproject/pull/11665/commits/4c03e96b24df32de559210b30f04dc28076987dc), use `Capybara.modify_selector` to allow writing `expect(page).to(have_selector("input", value: "hello world"))`.
To do:
* [x] [ ] Fix it everywhere
* [x] Use `{}` instead of `do ... end` everywhereÂ
* [x] [ ] Add a rubocop cop to prevent people from doing the same mistake again.
```text
expect(page).to have_selector("input") do |input|
input.value == "hello world"
end
```
is actually evaluated like
```text
expect(page).to(have_selector("input")) do
input.value == "hello world"
end
```
Meaning the block is an argument to the `to` method. Looking into rspec source code, the block is later discarded. So the value is never actually checked.
I fixed it in a Pull Request to Capybara [https://github.com/teamcapybara/capybara/pull/2617](https://github.com/teamcapybara/capybara/pull/2617)
In the meantime, we have to deal with it
Options:
* Using `{}` makes the code work as expected, but one-line readability can suffer.
* Using `within` and `have_field(with: "hello world")` can work too
* Like in [4c03e96b24](https://github.com/opf/openproject/pull/11665/commits/4c03e96b24df32de559210b30f04dc28076987dc), use `Capybara.modify_selector` to allow writing `expect(page).to(have_selector("input", value: "hello world"))`.
To do:
* [x]
* [x] Use `{}` instead of `do ... end`
* [x]