Skip to content

Commit 8b47470

Browse files
committed
fixup! rearrange conditionals in fixture support
1 parent 71934df commit 8b47470

File tree

3 files changed

+43
-23
lines changed

3 files changed

+43
-23
lines changed

lib/generators/rspec/install/templates/spec/rails_helper.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@
4242
# instead of true.
4343
config.use_transactional_fixtures = true
4444
45-
# You can uncomment this line to turn off active_record support entirely.
45+
# You can uncomment this line to turn off ActiveRecord support entirely.
4646
# config.use_active_record = false
4747
4848
<% else -%>
49-
# Remove this line to enable support for active_record
49+
# Remove this line to enable support for ActiveRecord
5050
config.use_active_record = false
5151
52-
# If you enable active_record support you shound unncomment these lines,
52+
# If you enable ActiveRecord support you should unncomment these lines,
5353
# note if you'd prefer not to run each example within a transaction, you
5454
# should set use_transactional_fixtures to false.
5555
#

lib/rspec/rails/fixture_support.rb

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,22 @@ module FixtureSupport
1010
include ActiveRecord::TestFixtures
1111

1212
included do
13-
FixtureSupport.setup if RSpec.configuration.use_active_record?
14-
end
13+
if RSpec.configuration.use_active_record?
14+
include Fixtures
15+
16+
self.fixture_path = RSpec.configuration.fixture_path
17+
if ::Rails::VERSION::STRING > '5'
18+
self.use_transactional_tests = RSpec.configuration.use_transactional_fixtures
19+
else
20+
self.use_transactional_fixtures = RSpec.configuration.use_transactional_fixtures
21+
end
22+
self.use_instantiated_fixtures = RSpec.configuration.use_instantiated_fixtures
1523

16-
def self.setup
17-
self.fixture_path = RSpec.configuration.fixture_path
18-
if ::Rails::VERSION::STRING > '5'
19-
self.use_transactional_tests = RSpec.configuration.use_transactional_fixtures
20-
else
21-
self.use_transactional_fixtures = RSpec.configuration.use_transactional_fixtures
24+
fixtures RSpec.configuration.global_fixtures if RSpec.configuration.global_fixtures
2225
end
23-
self.use_instantiated_fixtures = RSpec.configuration.use_instantiated_fixtures
26+
end
2427

28+
module Fixtures
2529
def self.fixtures(*args)
2630
orig_methods = private_instance_methods
2731
super.tap do
@@ -48,8 +52,6 @@ def name
4852
@example
4953
end
5054
end
51-
52-
fixtures RSpec.configuration.global_fixtures if RSpec.configuration.global_fixtures
5355
end
5456
end
5557
end

spec/generators/rspec/install/install_generator_spec.rb

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def content_for(file_name)
1212
end
1313

1414
def have_a_fixture_path
15-
match(/config\.fixture_path/m)
15+
match(/^ config\.fixture_path = /m)
1616
end
1717

1818
def maintain_test_schema
@@ -23,8 +23,16 @@ def require_rspec_rails
2323
match(/^require 'rspec\/rails'$/m)
2424
end
2525

26-
def have_transactional_fixtures_enabled?
27-
match(/config\.use_transactional_fixtures/m)
26+
def have_active_record_enabled
27+
match(/^\ # config\.use_active_record = false/m)
28+
end
29+
30+
def have_active_record_disabled
31+
match(/^\ config\.use_active_record = false/m)
32+
end
33+
34+
def have_transactional_fixtures_enabled
35+
match(/^ config\.use_transactional_fixtures = true/m)
2836
end
2937

3038
def filter_rails_from_backtrace
@@ -59,19 +67,24 @@ def filter_rails_from_backtrace
5967
expect(rails_helper).to require_rspec_rails
6068
end
6169

62-
specify "with transactional fixtures" do
70+
specify "with ActiveRecord" do
6371
run_generator
64-
expect(rails_helper).to have_a_fixture_path
72+
expect(rails_helper).to have_active_record_enabled
73+
expect(rails_helper).not_to have_active_record_disabled
6574
end
6675

6776
specify "with default fixture path" do
6877
run_generator
69-
expect(rails_helper).to have_transactional_fixtures_enabled?
78+
expect(rails_helper).to have_a_fixture_path
7079
end
7180

72-
specify "excluding rails gems from the backtrace" do
81+
specify "with transactional fixtures" do
7382
run_generator
83+
expect(rails_helper).to have_transactional_fixtures_enabled
84+
end
7485

86+
specify "excluding rails gems from the backtrace" do
87+
run_generator
7588
expect(rails_helper).to filter_rails_from_backtrace
7689
end
7790

@@ -91,14 +104,20 @@ def filter_rails_from_backtrace
91104
expect(rails_helper).to require_rspec_rails
92105
end
93106

107+
it "does not include config.use_active_record" do
108+
run_generator
109+
expect(rails_helper).not_to have_active_record_enabled
110+
expect(rails_helper).to have_active_record_disabled
111+
end
112+
94113
it "does not include config.fixture_path" do
95114
run_generator
96115
expect(rails_helper).not_to have_a_fixture_path
97116
end
98117

99118
it "does not include config.use_transactional_fixtures" do
100119
run_generator
101-
expect(rails_helper).not_to have_transactional_fixtures_enabled?
120+
expect(rails_helper).not_to have_transactional_fixtures_enabled
102121
end
103122

104123
it "does not check use active record migration options" do
@@ -107,5 +126,4 @@ def filter_rails_from_backtrace
107126
expect(rails_helper).not_to maintain_test_schema
108127
end
109128
end
110-
111129
end

0 commit comments

Comments
 (0)