Add and apply ConstantDefinitionInBlock rule

This rule was added in Rubocop 0.91.0. A similar rule named
LeakyConstantDeclaration was added in rubocop-rspec 1.34.0.

Note using the FILENAMES constant did not result in an offense using the
ConstantDefinitionInBlock rule but did result in an offense using the
LeakyConstantDeclaration rule. I've simplified the code to get rid of
the constant; not sure why we were adding a constant with `||=` in the
middle of a spec.
This commit is contained in:
Javi Martín
2020-10-14 12:03:26 +02:00
parent eb02bc756a
commit b27d0a8e92
5 changed files with 31 additions and 17 deletions

View File

@@ -166,6 +166,9 @@ Layout/TrailingWhitespace:
Lint/AmbiguousRegexpLiteral: Lint/AmbiguousRegexpLiteral:
Enabled: true Enabled: true
Lint/ConstantDefinitionInBlock:
Enabled: true
Lint/DuplicateMethods: Lint/DuplicateMethods:
Enabled: true Enabled: true

View File

@@ -1,12 +1,16 @@
require "rails_helper" require "rails_helper"
describe ConsulFormBuilder do describe ConsulFormBuilder do
class DummyModel before do
dummy_model = Class.new do
include ActiveModel::Model include ActiveModel::Model
OPTIONS = %w[Good Bad Ugly].freeze
attr_accessor :title, :quality attr_accessor :title, :quality
end end
stub_const("DummyModel", dummy_model)
stub_const("DummyModel::OPTIONS", %w[Good Bad Ugly].freeze)
end
let(:builder) { ConsulFormBuilder.new(:dummy, DummyModel.new, ActionView::Base.new, {}) } let(:builder) { ConsulFormBuilder.new(:dummy, DummyModel.new, ActionView::Base.new, {}) }
describe "hints" do describe "hints" do

View File

@@ -1,7 +1,8 @@
require "rails_helper" require "rails_helper"
describe Statisticable do describe Statisticable do
class DummyStats before do
dummy_stats = Class.new do
include Statisticable include Statisticable
def participants def participants
@@ -9,6 +10,9 @@ describe Statisticable do
end end
end end
stub_const("DummyStats", dummy_stats)
end
let(:stats) { DummyStats.new(nil) } let(:stats) { DummyStats.new(nil) }
describe "#gender?" do describe "#gender?" do

View File

@@ -235,12 +235,11 @@ shared_examples "nested documentable" do |login_as_name, documentable_factory_na
end end
login_as user_to_login login_as user_to_login
visit send(path, arguments) visit send(path, arguments)
FILENAMES ||= %w[clippy empty logo].freeze
send(fill_resource_method_name) if fill_resource_method_name send(fill_resource_method_name) if fill_resource_method_name
documentable.class.max_documents_allowed.times.zip(FILENAMES).each do |_n, fn| %w[clippy empty logo].take(documentable.class.max_documents_allowed).each do |filename|
documentable_attach_new_file(Rails.root.join("spec/fixtures/files/#{fn}.pdf")) documentable_attach_new_file(Rails.root.join("spec/fixtures/files/#{filename}.pdf"))
end end
click_on submit_button click_on submit_button

View File

@@ -1,7 +1,8 @@
require "rails_helper" require "rails_helper"
describe "shared errors" do describe "shared errors" do
class DummyModel before do
dummy_model = Class.new do
include ActiveModel::Model include ActiveModel::Model
attr_accessor :title, :description, :days attr_accessor :title, :description, :days
@@ -10,6 +11,9 @@ describe "shared errors" do
validates :days, numericality: { greater_than: 10 } validates :days, numericality: { greater_than: 10 }
end end
stub_const("DummyModel", dummy_model)
end
it "counts the number of fields with errors" do it "counts the number of fields with errors" do
resource = DummyModel.new(title: "Present", description: "", days: 3) resource = DummyModel.new(title: "Present", description: "", days: 3)
resource.valid? resource.valid?