diff --git a/rubocop/cop/rspec/factories_in_migration_specs.rb b/rubocop/cop/rspec/factories_in_migration_specs.rb index 7dce1264b0e15900cf8fd1042598f008ca7fb00c..cf90b768a22d96403edfcc0a82e3a0cd6c10706b 100644 --- a/rubocop/cop/rspec/factories_in_migration_specs.rb +++ b/rubocop/cop/rspec/factories_in_migration_specs.rb @@ -20,7 +20,7 @@ class FactoriesInMigrationSpecs < RuboCop::Cop::Base FORBIDDEN_METHODS = %i[build build_list create create_list attributes_for].freeze def_node_search :forbidden_factory_usage?, <<~PATTERN - (send {(const nil? :FactoryBot) nil?} {#{FORBIDDEN_METHODS.map(&:inspect).join(' ')}} ...) + (send {(const nil? :FactoryBot) nil?} {#{FORBIDDEN_METHODS.map(&:inspect).join(' ')}} _ ...) PATTERN # Following is what node.children looks like on a match: diff --git a/spec/rubocop/cop/rspec/factories_in_migration_specs_spec.rb b/spec/rubocop/cop/rspec/factories_in_migration_specs_spec.rb index e41dd338387d53a10fb844a54f947673bf16980d..29964d68cf6ca3e7b5a1ae048a77c6f710e297fc 100644 --- a/spec/rubocop/cop/rspec/factories_in_migration_specs_spec.rb +++ b/spec/rubocop/cop/rspec/factories_in_migration_specs_spec.rb @@ -7,15 +7,34 @@ RSpec.describe RuboCop::Cop::RSpec::FactoriesInMigrationSpecs do shared_examples 'an offensive factory call' do |namespace| %i[build build_list create create_list attributes_for].each do |forbidden_method| - namespaced_forbidden_method = "#{namespace}#{forbidden_method}(:user)" + namespaced_forbidden_method = "#{namespace}#{forbidden_method}" - it "registers an offense for #{namespaced_forbidden_method}" do - expect_offense(<<-RUBY) + it "registers an offense for #{namespaced_forbidden_method} with one arg" do + code = <<-RUBY describe 'foo' do - let(:user) { #{namespaced_forbidden_method} } - #{'^' * namespaced_forbidden_method.size} Don't use FactoryBot.#{forbidden_method} in migration specs, use `table` instead. + let(:user) { %{namespaced_method}(:user) } + ^{namespaced_method}^^^^^^^ Don't use FactoryBot.%{method} in migration specs, use `table` instead. end RUBY + + expect_offense(code, method: forbidden_method, namespaced_method: namespaced_forbidden_method) + end + + it "registers an offense for #{namespaced_forbidden_method} with multiple args" do + code = <<-RUBY + describe 'foo' do + let(:user) { %{namespaced_method}(:user, name: name) } + ^{namespaced_method}^^^^^^^^^^^^^^^^^^^ Don't use FactoryBot.%{method} in migration specs, use `table` instead. + end + RUBY + + expect_offense(code, method: forbidden_method, namespaced_method: namespaced_forbidden_method) + end + + it "does not create an offense for #{namespaced_forbidden_method} with no args" do + expect_no_offenses(<<~RUBY) + let(:id) { #{namespaced_forbidden_method}.id } + RUBY end end end