summaryrefslogtreecommitdiff
path: root/spec/rubocop
diff options
context:
space:
mode:
authorRémy Coutable <remy@rymai.me>2018-03-30 17:18:12 +0200
committerRémy Coutable <remy@rymai.me>2018-04-06 17:41:52 +0200
commit4b035896c4ed188c0b4ab0e0b5d1d97e4fef9886 (patch)
treefe2c72f9a850a1d16f05e05b85f82cd090b57c7f /spec/rubocop
parent0fff9db5eac32ae42cc06e31447104ae15c93675 (diff)
downloadgitlab-ce-4b035896c4ed188c0b4ab0e0b5d1d97e4fef9886.tar.gz
Introduce a new FactoriesInMigrationSpecs cop
Signed-off-by: Rémy Coutable <remy@rymai.me>
Diffstat (limited to 'spec/rubocop')
-rw-r--r--spec/rubocop/cop/rspec/factories_in_migration_specs_spec.rb48
1 files changed, 48 insertions, 0 deletions
diff --git a/spec/rubocop/cop/rspec/factories_in_migration_specs_spec.rb b/spec/rubocop/cop/rspec/factories_in_migration_specs_spec.rb
new file mode 100644
index 00000000000..2763f2bda21
--- /dev/null
+++ b/spec/rubocop/cop/rspec/factories_in_migration_specs_spec.rb
@@ -0,0 +1,48 @@
+require 'spec_helper'
+
+require 'rubocop'
+require 'rubocop/rspec/support'
+
+require_relative '../../../../rubocop/cop/rspec/factories_in_migration_specs'
+
+describe RuboCop::Cop::RSpec::FactoriesInMigrationSpecs do
+ include CopHelper
+
+ let(:source_file) { 'spec/migrations/foo_spec.rb' }
+
+ subject(:cop) { described_class.new }
+
+ shared_examples 'an offensive factory call' do |namespace|
+ %i[build build_list create create_list].each do |forbidden_method|
+ namespaced_forbidden_method = "#{namespace}#{forbidden_method}(:user)"
+
+ it "registers an offense for #{namespaced_forbidden_method}" do
+ expect_offense(<<-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.
+ end
+ RUBY
+ end
+ end
+ end
+
+ context 'in a migration spec file' do
+ before do
+ allow(cop).to receive(:in_migration_spec?).and_return(true)
+ end
+
+ it_behaves_like 'an offensive factory call', ''
+ it_behaves_like 'an offensive factory call', 'FactoryBot.'
+ end
+
+ context 'outside of a migration spec file' do
+ it "does not register an offense" do
+ expect_no_offenses(<<-RUBY)
+ describe 'foo' do
+ let(:user) { create(:user) }
+ end
+ RUBY
+ end
+ end
+end