Skip to content

Commit bdb3491

Browse files
committed
Refactor bootstrap specs
1 parent a217af3 commit bdb3491

File tree

3 files changed

+61
-32
lines changed

3 files changed

+61
-32
lines changed

spec/spec_helper.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ def verify_events
6161
yield
6262
end
6363
end
64+
65+
def undefined
66+
double('undefined')
67+
end
6468
end # XSpecHelper
6569

6670
RSpec.configure do |config|

spec/unit/mutant/cli_spec.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
let(:stderr) { instance_double(IO, 'stderr', puts: undefined) }
77
let(:stdout) { instance_double(IO, 'stdout', puts: undefined) }
88
let(:target_stream) { stdout }
9-
let(:undefined) { instance_double('undefined') }
109

1110
let(:config) do
1211
default_config.with(

spec/unit/mutant/env/bootstrap_spec.rb

Lines changed: 57 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
let(:matcher_config) { Mutant::Matcher::Config::DEFAULT }
99
let(:object_space) { class_double(ObjectSpace) }
1010
let(:object_space_modules) { [] }
11+
let(:parser) { instance_double(Mutant::Parser) }
1112

1213
let(:world) do
1314
instance_double(
@@ -43,39 +44,65 @@
4344
)
4445
end
4546

47+
shared_examples 'expected warning' do
48+
let(:warns) { [] }
49+
50+
before do
51+
allow(config.reporter).to receive(:warn, &warns.method(:<<))
52+
end
53+
54+
it 'warns with expected warning' do
55+
expect { apply }.to change(warns, :to_a).from([]).to([expected_warning])
56+
end
57+
end
58+
4659
shared_examples_for 'bootstrap call' do
4760
it 'returns expected env' do
4861
expect(apply).to eql(expected_env)
4962
end
50-
end
5163

52-
def expect_warning
53-
expect(config.reporter).to receive(:warn)
54-
.with(expected_warning)
55-
.and_return(config.reporter)
64+
it 'performs IO in expected sequence' do
65+
apply
66+
67+
expect(object_space)
68+
.to have_received(:each_object)
69+
.with(Module)
70+
.ordered
71+
72+
expect(integration_class)
73+
.to have_received(:new)
74+
.with(config)
75+
.ordered
76+
77+
expect(integration)
78+
.to have_received(:setup)
79+
.ordered
80+
end
5681
end
5782

5883
before do
59-
expect(integration_class).to receive(:new)
60-
.with(config)
61-
.and_return(integration)
62-
63-
expect(integration).to receive_messages(setup: integration)
84+
allow(integration_class).to receive_messages(new: integration)
85+
allow(integration).to receive_messages(setup: integration)
6486

65-
expect(object_space).to receive(:each_object)
66-
.with(Module)
67-
.and_return(object_space_modules.each)
87+
allow(object_space).to receive_messages(
88+
each_object: object_space_modules.each
89+
)
6890
end
6991

7092
describe '#warn' do
71-
let(:object) { described_class.new(world, config) }
72-
let(:expected_warning) { instance_double(String) }
93+
subject { described_class.new(world, config) }
7394

74-
subject { object.warn(expected_warning) }
95+
def apply
96+
subject.warn(expected_warning)
97+
end
7598

76-
before { expect_warning }
99+
let(:expected_warning) { instance_double(String) }
77100

78-
it_behaves_like 'a command method'
101+
include_examples 'expected warning'
102+
103+
it 'returns self' do
104+
expect(apply).to be(subject)
105+
end
79106
end
80107

81108
describe '.call' do
@@ -87,27 +114,27 @@ def apply
87114
let(:object_space_modules) { [invalid_class] }
88115

89116
let(:expected_warning) do
90-
"Class#name from: #{invalid_class} raised an error: " \
117+
"Object#name from: #{invalid_class} raised an error: " \
91118
"RuntimeError. #{Mutant::Env::SEMANTICS_MESSAGE}"
92119
end
93120

121+
# Not really a class, but does not leak a "wrong" class
122+
# into later specs.
94123
let(:invalid_class) do
95-
Class.new do
96-
def self.name
124+
Object.new.tap do |object|
125+
def object.name
97126
fail
98127
end
99128
end
100129
end
101130

102-
after do
103-
# Fix Class#name so other specs do not see this one
104-
class << invalid_class
105-
undef :name
106-
def name; end
107-
end
108-
end
131+
include_examples 'expected warning'
132+
include_examples 'bootstrap call'
133+
end
109134

110-
before { expect_warning }
135+
context 'when Module#name calls return nil' do
136+
let(:anonymous_class) { Class.new }
137+
let(:object_space_modules) { [anonymous_class] }
111138

112139
include_examples 'bootstrap call'
113140
end
@@ -164,8 +191,7 @@ def name; end
164191
end
165192
end
166193

167-
before { expect_warning }
168-
194+
include_examples 'expected warning'
169195
include_examples 'bootstrap call'
170196
end
171197

0 commit comments

Comments
 (0)