Sha256: 9dd6476ccfa4887b6449e38162b084d4e386c451394ff5639ed213ad90c1c765

Contents?: true

Size: 1.5 KB

Versions: 28

Compression:

Stored size: 1.5 KB

Contents

# frozen_string_literal: true

require 'spec_helper'

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

  it 'registers an offense for overwriting $stdout within an example' do
    expect_offense(<<-RUBY)
      specify do
        $stdout = StringIO.new
        ^^^^^^^ Use `expect { ... }.to output(...).to_stdout` instead of mutating $stdout.
      end
    RUBY
  end

  it 'registers an offense for overwriting $stderr ' \
     'within an example scoped hook' do
    expect_offense(<<-RUBY)
      before(:each) do
        $stderr = StringIO.new
        ^^^^^^^ Use `expect { ... }.to output(...).to_stderr` instead of mutating $stderr.
      end
    RUBY
  end

  it 'does not register an offense for interacting with $stdout' do
    expect_no_offenses(<<-RUBY)
      specify do
        $stdout.puts("hi")
      end
    RUBY
  end

  it 'does not flag assignments to other global variables' do
    expect_no_offenses(<<-RUBY)
      specify do
        $blah = StringIO.new
      end
    RUBY
  end

  it 'does not flag assignments to $stdout outside of example scope' do
    expect_no_offenses(<<-RUBY)
      before(:suite) do
        $stderr = StringIO.new
      end
    RUBY
  end

  it 'does not flag assignments to $stdout in example_group scope' do
    expect_no_offenses(<<-RUBY)
      describe Foo do
        $stderr = StringIO.new
      end
    RUBY
  end

  it 'does not flag assigns to $stdout when in the root scope' do
    expect_no_offenses('$stderr = StringIO.new')
  end
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
rubocop-rspec-1.21.0 spec/rubocop/cop/rspec/expect_output_spec.rb
rubocop-rspec-1.20.1 spec/rubocop/cop/rspec/expect_output_spec.rb
rubocop-rspec-1.20.0 spec/rubocop/cop/rspec/expect_output_spec.rb
rubocop-rspec-1.19.0 spec/rubocop/cop/rspec/expect_output_spec.rb
rubocop-rspec-1.18.0 spec/rubocop/cop/rspec/expect_output_spec.rb
rubocop-rspec-1.17.1 spec/rubocop/cop/rspec/expect_output_spec.rb
rubocop-rspec-1.17.0 spec/rubocop/cop/rspec/expect_output_spec.rb
rubocop-rspec-1.16.0 spec/rubocop/cop/rspec/expect_output_spec.rb