RSpec/Rails

RSpec/Rails/AvoidSetupHook

Enabled by default Safe Supports autocorrection Version Added Version Changed

Pending

Yes

Yes

2.4

-

Checks that tests use RSpec before hook over Rails setup method.

Examples

# bad
setup do
  allow(foo).to receive(:bar)
end

# good
before do
  allow(foo).to receive(:bar)
end

RSpec/Rails/HaveHttpStatus

Enabled by default Safe Supports autocorrection Version Added Version Changed

Pending

Yes

Yes (Unsafe)

2.12

-

Checks that tests use have_http_status instead of equality matchers.

Examples

# bad
expect(response.status).to be(200)

# good
expect(response).to have_http_status(200)

RSpec/Rails/HttpStatus

Enabled by default Safe Supports autocorrection Version Added Version Changed

Enabled

Yes

Yes

1.23

2.0

Enforces use of symbolic or numeric value to describe HTTP status.

Examples

EnforcedStyle: symbolic (default)

# bad
it { is_expected.to have_http_status 200 }
it { is_expected.to have_http_status 404 }

# good
it { is_expected.to have_http_status :ok }
it { is_expected.to have_http_status :not_found }
it { is_expected.to have_http_status :success }
it { is_expected.to have_http_status :error }

EnforcedStyle: numeric

# bad
it { is_expected.to have_http_status :ok }
it { is_expected.to have_http_status :not_found }

# good
it { is_expected.to have_http_status 200 }
it { is_expected.to have_http_status 404 }
it { is_expected.to have_http_status :success }
it { is_expected.to have_http_status :error }

Configurable attributes

Name Default value Configurable values

EnforcedStyle

symbolic

numeric, symbolic

RSpec/Rails/InferredSpecType

Enabled by default Safe Supports autocorrection Version Added Version Changed

Pending

No

Yes (Unsafe)

2.14

-

Identifies redundant spec type.

After setting up rspec-rails, you will have enabled config.infer_spec_type_from_file_location! by default in spec/rails_helper.rb. This cop works in conjunction with this config. If you disable this config, disable this cop as well.

Safety

This cop is marked as unsafe because config.infer_spec_type_from_file_location! may not be enabled.

Examples

# bad
# spec/models/user_spec.rb
RSpec.describe User, type: :model do
end

# good
# spec/models/user_spec.rb
RSpec.describe User do
end

# good
# spec/models/user_spec.rb
RSpec.describe User, type: :common do
end

Inferences configuration

# .rubocop.yml
# RSpec/InferredSpecType:
#   Inferences:
#     services: service

# bad
# spec/services/user_spec.rb
RSpec.describe User, type: :service do
end

# good
# spec/services/user_spec.rb
RSpec.describe User do
end

# good
# spec/services/user_spec.rb
RSpec.describe User, type: :common do
end

Configurable attributes

Name Default value Configurable values

Inferences

{"channels"⇒"channel", "controllers"⇒"controller", "features"⇒"feature", "generator"⇒"generator", "helpers"⇒"helper", "jobs"⇒"job", "mailboxes"⇒"mailbox", "mailers"⇒"mailer", "models"⇒"model", "requests"⇒"request", "integration"⇒"request", "api"⇒"request", "routing"⇒"routing", "system"⇒"system", "views"⇒"view"}