Sha256: d5d6f61ec932ea221c4a83d4e8c64d9998bb389f9ec276f822ea022afc4f4bd2

Contents?: true

Size: 1.69 KB

Versions: 5

Compression:

Stored size: 1.69 KB

Contents

# frozen_string_literal: true

require 'spec_helper'

module RailsBestPractices
  module Reviews
    describe DefaultScopeIsEvilReview do
      let(:runner) { Core::Runner.new(reviews: DefaultScopeIsEvilReview.new) }

      it 'should detect default_scope with -> syntax' do
        content = <<-EOF
        class User < ActiveRecord::Base
          default_scope -> { order('created_at desc') }
        end
        EOF
        runner.review('app/models/user.rb', content)
        expect(runner.errors.size).to eq(1)
        expect(runner.errors[0].to_s).to eq('app/models/user.rb:2 - default_scope is evil')
      end

      it 'should detect default_scope with old syntax' do
        content = <<-EOF
        class User < ActiveRecord::Base
          default_scope order('created_at desc')
        end
        EOF
        runner.review('app/models/user.rb', content)
        expect(runner.errors.size).to eq(1)
        expect(runner.errors[0].to_s).to eq('app/models/user.rb:2 - default_scope is evil')
      end

      it 'should not detect default_scope' do
        content = <<-EOF
        class User < ActiveRecord::Base
          scope :default, -> { order('created_at desc') }
        end
        EOF
        runner.review('app/models/user.rb', content)
        expect(runner.errors.size).to eq(0)
      end

      it 'should not check ignored files' do
        runner = Core::Runner.new(reviews: DefaultScopeIsEvilReview.new(ignored_files: /user/))
        content = <<-EOF
        class User < ActiveRecord::Base
          default_scope -> { order('created_at desc') }
        end
        EOF
        runner.review('app/models/user.rb', content)
        expect(runner.errors.size).to eq(0)
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rails_best_practices-1.20.0 spec/rails_best_practices/reviews/default_scope_is_evil_review_spec.rb
rails_best_practices-1.19.5 spec/rails_best_practices/reviews/default_scope_is_evil_review_spec.rb
rails_best_practices-1.19.4 spec/rails_best_practices/reviews/default_scope_is_evil_review_spec.rb
rails_best_practices-1.19.3 spec/rails_best_practices/reviews/default_scope_is_evil_review_spec.rb
rails_best_practices-1.19.2 spec/rails_best_practices/reviews/default_scope_is_evil_review_spec.rb