Capybara/RSpec

Capybara/RSpec/HaveSelector

Enabled by default Safe Supports autocorrection Version Added Version Changed

Pending

Yes

Yes

2.19

-

Use have_css or have_xpath instead of have_selector.

Examples

# bad
expect(foo).to have_selector(:css, 'bar')

# good
expect(foo).to have_css('bar')

# bad
expect(foo).to have_selector(:xpath, 'bar')

# good
expect(foo).to have_xpath('bar')

DefaultSelector: css (default)

# bad
expect(foo).to have_selector('bar')

# good
expect(foo).to have_css('bar')

DefaultSelector: xpath

# bad
expect(foo).to have_selector('bar')

# good
expect(foo).to have_xpath('bar')

Configurable attributes

Name Default value Configurable values

DefaultSelector

css

String

Capybara/RSpec/PredicateMatcher

Enabled by default Safe Supports autocorrection Version Added Version Changed

Pending

Yes

Yes

2.19

-

Prefer using predicate matcher over using predicate method directly.

Capybara defines magic matchers for predicate methods. This cop recommends to use the predicate matcher instead of using predicate method directly.

Examples

Strict: true, EnforcedStyle: inflected (default)

# bad
expect(foo.matches_css?(bar: 'baz')).to be_truthy
expect(foo.matches_selector?(bar: 'baz')).to be_truthy
expect(foo.matches_style?(bar: 'baz')).to be_truthy
expect(foo.matches_xpath?(bar: 'baz')).to be_truthy

# good
expect(foo).to match_css(bar: 'baz')
expect(foo).to match_selector(bar: 'baz')
expect(foo).to match_style(bar: 'baz')
expect(foo).to match_xpath(bar: 'baz')

# also good - It checks "true" strictly.
expect(foo.matches_style?(bar: 'baz')).to be(true)

Strict: false, EnforcedStyle: inflected

# bad
expect(foo.matches_style?(bar: 'baz')).to be_truthy
expect(foo.matches_style?(bar: 'baz')).to be(true)

# good
expect(foo).to match_style(bar: 'baz')

Strict: true, EnforcedStyle: explicit

# bad
expect(foo).to match_style(bar: 'baz')

# good - the above code is rewritten to it by this cop
expect(foo.matches_style?(bar: 'baz')).to be(true)

Strict: false, EnforcedStyle: explicit

# bad
expect(foo).to match_style(bar: 'baz')

# good - the above code is rewritten to it by this cop
expect(foo.matches_style?(bar: 'baz')).to be_truthy

Configurable attributes

Name Default value Configurable values

Strict

true

Boolean

EnforcedStyle

inflected

inflected, explicit

AllowedExplicitMatchers

[]

Array