Sha256: 91b75d5456540cc3b49e072e7233424979ada405121c4856032eb978b2d9c663

Contents?: true

Size: 1.64 KB

Versions: 1

Compression:

Stored size: 1.64 KB

Contents

describe RuboCop::Cop::RSpec::InstanceSpy do
  subject(:cop) { described_class.new }

  context 'when used with `have_received`' do
    it 'adds an offense for an instance_double with single argument' do
      expect_violation(<<-RUBY)
        it do
          foo = instance_double(Foo).as_null_object
                ^^^^^^^^^^^^^^^^^^^^ Use `instance_spy` when you check your double with `have_received`
          expect(foo).to have_received(:bar)
        end
      RUBY
    end

    it 'adds an offense for an instance_double with multiple arguments' do
      expect_violation(<<-RUBY)
        it do
          foo = instance_double(Foo, :name).as_null_object
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `instance_spy` when you check your double with `have_received`
          expect(foo).to have_received(:bar)
        end
      RUBY
    end

    it 'ignores instance_double when it is not used with as_null_object' do
      expect_no_violations(<<-RUBY)
        it do
          foo = instance_double(Foo)
          expect(bar).to have_received(:bar)
       end
      RUBY
    end
  end

  context 'when not used with `have_received`' do
    it 'does not add an offence' do
      expect_no_violations(<<-RUBY)
        it do
          foo = instance_double(Foo).as_null_object
          expect(bar).to have_received(:bar)
        end
      RUBY
    end
  end

  original = <<-RUBY
  it do
    foo = instance_double(Foo, :name).as_null_object
    expect(foo).to have_received(:bar)
  end
  RUBY
  corrected = <<-RUBY
  it do
    foo = instance_spy(Foo, :name)
    expect(foo).to have_received(:bar)
  end
  RUBY

  include_examples 'autocorrect', original, corrected
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rubocop-rspec-1.12.0 spec/rubocop/cop/rspec/instance_spy_spec.rb