Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cop idea: disallow using exact_text when Capybara.exact = true #6

Open
boris-petrov opened this issue Jul 22, 2022 · 2 comments
Open
Labels

Comments

@boris-petrov
Copy link
Contributor

Capybara.exact = true
expect(page).to have_link(exact_text: 'foobar')

It would be nice to have a check that disallows passing exact_text to such matchers as that's unneeded - Capybara.exact makes these matcher work like so by default. That is, the above is equivalent to expect(page).to have_link('foobar').

A couple of things to note:

  1. this is perhaps difficult to implement as Capybara.exact = true is generally set in a different file spec_helper.rb. Perhaps an option to the plugin could be passed, not sure?
  2. there are exceptions to this. If the text contains new lines, exact_text must be passed (because of Inconsistency between has_css? and find_link teamcapybara/capybara#2533).
@bquorning
Copy link
Contributor

  1. I think we’d need to implement this via the cop configuration. The Capybara.exact= is dynamic code which is not executed by RuboCop and, as you mention, may not exist in the same file as the offense.

Just to be sure I understand this – is it correct that when Capybara.exact = true is set, we should change

expect(page).to have_link(exact_text: 'foobar')

into

expect(page).to have_link('foobar')

But the following should not be changed:

expect(page).to have_link(exact_text: <<~TEXT)
  foo
  bar
TEXT

@boris-petrov
Copy link
Contributor Author

boris-petrov commented Jul 28, 2022

@bquorning sorry for the late reply.

Yes, that's correct. Also expect(page).to have_link(exact_text: "foo\nbar") shouldn't be changed. Also expect(page).to have_link(text: 'foobar') should not be changed because that doesn't look for an exact match but a partial one.

And yes, a cop-configuration is perhaps the way to go for this.

P.S. Code similar to find('td', exact_text: 'foo') and expect(page).to have_css('.some-class', exact_text: 'foo') also shouldn't be changed.

@pirj pirj transferred this issue from rubocop/rubocop-rspec Dec 29, 2022
@ydah ydah added the cop label Feb 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants