debian-mirror-gitlab/rubocop/cop/rspec/factories_in_migration_specs.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

40 lines
1.2 KiB
Ruby
Raw Normal View History

2019-12-04 20:38:33 +05:30
# frozen_string_literal: true
2018-05-09 12:01:36 +05:30
2022-11-25 23:54:43 +05:30
require 'rubocop-rspec'
2018-05-09 12:01:36 +05:30
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') }
2022-10-11 01:57:18 +05:30
class FactoriesInMigrationSpecs < RuboCop::Cop::Base
2021-04-29 21:17:54 +05:30
MESSAGE = "Don't use FactoryBot.%s in migration specs, use `table` instead."
2021-03-11 19:13:27 +05:30
FORBIDDEN_METHODS = %i[build build_list create create_list attributes_for].freeze
2018-05-09 12:01:36 +05:30
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 forbidden_factory_usage?(node)
method = node.children[1]
2022-10-11 01:57:18 +05:30
add_offense(node, message: MESSAGE % method)
2018-05-09 12:01:36 +05:30
end
end
end
end
end