/
expect_output_spec.rb
60 lines (52 loc) · 1.46 KB
/
expect_output_spec.rb
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
49
50
51
52
53
54
55
56
57
58
59
60
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe RuboCop::Cop::RSpec::ExpectOutput do
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