summaryrefslogtreecommitdiff
path: root/spec/rubocop/cop/default_scope_spec.rb
blob: fee1895603c0aca2bfcf7f2c5f0f15c585531fd3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# frozen_string_literal: true

require 'fast_spec_helper'
require 'rubocop'
require 'rubocop/rspec/support'
require_relative '../../../rubocop/cop/default_scope'

RSpec.describe RuboCop::Cop::DefaultScope do
  include CopHelper

  subject(:cop) { described_class.new }

  it 'does not flag the use of default_scope with a send receiver' do
    inspect_source('foo.default_scope')

    expect(cop.offenses.size).to eq(0)
  end

  it 'flags the use of default_scope with a constant receiver' do
    inspect_source('User.default_scope')

    expect(cop.offenses.size).to eq(1)
  end

  it 'flags the use of default_scope with a nil receiver' do
    inspect_source('class Foo ; default_scope ; end')

    expect(cop.offenses.size).to eq(1)
  end

  it 'flags the use of default_scope when passing arguments' do
    inspect_source('class Foo ; default_scope(:foo) ; end')

    expect(cop.offenses.size).to eq(1)
  end

  it 'flags the use of default_scope when passing a block' do
    inspect_source('class Foo ; default_scope { :foo } ; end')

    expect(cop.offenses.size).to eq(1)
  end

  it 'ignores the use of default_scope with a local variable receiver' do
    inspect_source('users = User.all ; users.default_scope')

    expect(cop.offenses.size).to eq(0)
  end
end