From 4b035896c4ed188c0b4ab0e0b5d1d97e4fef9886 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Coutable?= Date: Fri, 30 Mar 2018 17:18:12 +0200 Subject: Introduce a new FactoriesInMigrationSpecs cop MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Rémy Coutable --- rubocop/cop/rspec/factories_in_migration_specs.rb | 40 +++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 rubocop/cop/rspec/factories_in_migration_specs.rb (limited to 'rubocop/cop/rspec/factories_in_migration_specs.rb') diff --git a/rubocop/cop/rspec/factories_in_migration_specs.rb b/rubocop/cop/rspec/factories_in_migration_specs.rb new file mode 100644 index 00000000000..0c5aa838a2c --- /dev/null +++ b/rubocop/cop/rspec/factories_in_migration_specs.rb @@ -0,0 +1,40 @@ +require_relative '../../spec_helpers' + +module RuboCop + module Cop + module RSpec + # This cop checks for the usage of factories in migration specs + # + # @example + # + # # bad + # let(:user) { create(:user) } + # + # # good + # let(:users) { table(:users) } + # let(:user) { users.create!(name: 'User 1', username: 'user1') } + class FactoriesInMigrationSpecs < RuboCop::Cop::Cop + include SpecHelpers + + MESSAGE = "Don't use FactoryBot.%s in migration specs, use `table` instead.".freeze + FORBIDDEN_METHODS = %i[build build_list create create_list].freeze + + def_node_search :forbidden_factory_usage?, <<~PATTERN + (send {(const nil? :FactoryBot) nil?} {#{FORBIDDEN_METHODS.map(&:inspect).join(' ')}} ...) + PATTERN + + # Following is what node.children looks like on a match: + # - Without FactoryBot namespace: [nil, :build, s(:sym, :user)] + # - With FactoryBot namespace: [s(:const, nil, :FactoryBot), :build, s(:sym, :user)] + def on_send(node) + return unless in_migration_spec?(node) + return unless forbidden_factory_usage?(node) + + method = node.children[1] + + add_offense(node, location: :expression, message: MESSAGE % method) + end + end + end + end +end -- cgit v1.2.1