Skip to content

Commit b04780f

Browse files
committed
Merge pull request #2719 from rspec/refactor-generator-specs
1 parent 525654e commit b04780f

13 files changed

+551
-376
lines changed

spec/generators/rspec/channel/channel_generator_spec.rb

+4-6
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@
55
RSpec.describe Rspec::Generators::ChannelGenerator, type: :generator, skip: !RSpec::Rails::FeatureCheck.has_action_cable_testing? do
66
setup_default_destination
77

8-
describe 'the generated files' do
9-
before { run_generator %w[chat] }
8+
before { run_generator %w[chat] }
109

11-
subject { file("spec/channels/chat_channel_spec.rb") }
10+
subject(:channel_spec) { file("spec/channels/chat_channel_spec.rb") }
1211

13-
it { is_expected.to exist }
14-
it { is_expected.to contain(/require 'rails_helper'/) }
15-
it { is_expected.to contain(/describe ChatChannel, #{type_metatag(:channel)}/) }
12+
it "generates a channel spec file" do
13+
expect(channel_spec).to contain(/require 'rails_helper'/).and(contain(/describe ChatChannel, #{type_metatag(:channel)}/))
1614
end
1715
end

spec/generators/rspec/controller/controller_generator_spec.rb

+95-51
Original file line numberDiff line numberDiff line change
@@ -6,49 +6,53 @@
66
setup_default_destination
77

88
describe 'request specs' do
9-
subject { file('spec/requests/posts_spec.rb') }
9+
subject(:filename) { file('spec/requests/posts_spec.rb') }
1010

1111
describe 'generated by default' do
1212
before do
1313
run_generator %w[posts]
1414
end
1515

16-
describe 'the spec' do
17-
it { is_expected.to exist }
18-
it { is_expected.to contain(/require 'rails_helper'/) }
19-
it { is_expected.to contain(/^RSpec.describe "Posts", #{type_metatag(:request)}/) }
20-
it { is_expected.to contain('pending') }
16+
it 'includes the standard boilerplate' do
17+
expect(filename).to contain(/require 'rails_helper'/)
18+
.and(contain(/^RSpec.describe "Posts", #{type_metatag(:request)}/))
19+
.and(contain('pending'))
2120
end
2221
end
2322

2423
describe 'skipped with a flag' do
2524
before do
2625
run_generator %w[posts --no-request_specs]
2726
end
28-
it { is_expected.not_to exist }
27+
28+
it 'skips the file' do
29+
expect(File.exist?(filename)).to be false
30+
end
2931
end
3032

3133
describe 'with actions' do
3234
before do
3335
run_generator %w[posts index custom_action]
3436
end
3537

36-
it { is_expected.to exist }
37-
it { is_expected.to contain('get "/posts/index"') }
38-
it { is_expected.to contain('get "/posts/custom_action"') }
38+
it 'includes the standard boilerplate' do
39+
expect(filename).to contain('get "/posts/index"')
40+
.and(contain('get "/posts/custom_action"'))
41+
end
3942
end
4043

4144
describe 'with namespace and actions' do
42-
subject { file('spec/requests/admin/external/users_spec.rb') }
45+
subject(:filename) { file('spec/requests/admin/external/users_spec.rb') }
4346

4447
before do
4548
run_generator %w[admin::external::users index custom_action]
4649
end
4750

48-
it { is_expected.to exist }
49-
it { is_expected.to contain(/^RSpec.describe "Admin::External::Users", #{type_metatag(:request)}/) }
50-
it { is_expected.to contain('get "/admin/external/users/index"') }
51-
it { is_expected.to contain('get "/admin/external/users/custom_action"') }
51+
it 'includes the standard boilerplate' do
52+
expect(filename).to contain(/^RSpec.describe "Admin::External::Users", #{type_metatag(:request)}/)
53+
.and(contain('get "/admin/external/users/index"'))
54+
.and(contain('get "/admin/external/users/custom_action"'))
55+
end
5256
end
5357
end
5458

@@ -58,18 +62,27 @@
5862
before do
5963
run_generator %w[posts index show --no-view-specs]
6064
end
65+
6166
describe 'index.html.erb' do
62-
subject { file('spec/views/posts/index.html.erb_spec.rb') }
63-
it { is_expected.not_to exist }
67+
subject(:filename) { file('spec/views/posts/index.html.erb_spec.rb') }
68+
69+
it 'skips the file' do
70+
expect(File.exist?(filename)).to be false
71+
end
6472
end
6573
end
74+
6675
describe 'with no actions' do
6776
before do
6877
run_generator %w[posts]
6978
end
79+
7080
describe 'index.html.erb' do
71-
subject { file('spec/views/posts/index.html.erb_spec.rb') }
72-
it { is_expected.not_to exist }
81+
subject(:filename) { file('spec/views/posts/index.html.erb_spec.rb') }
82+
83+
it 'skips the file' do
84+
expect(File.exist?(filename)).to be false
85+
end
7386
end
7487
end
7588

@@ -79,8 +92,11 @@
7992
end
8093

8194
describe 'index.html.erb' do
82-
subject { file('spec/views/posts/index.html._spec.rb') }
83-
it { is_expected.not_to exist }
95+
subject(:filename) { file('spec/views/posts/index.html._spec.rb') }
96+
97+
it 'skips the file' do
98+
expect(File.exist?(filename)).to be false
99+
end
84100
end
85101
end
86102
end
@@ -90,80 +106,104 @@
90106
before do
91107
run_generator %w[posts index show]
92108
end
109+
93110
describe 'index.html.erb' do
94-
subject { file('spec/views/posts/index.html.erb_spec.rb') }
95-
it { is_expected.to exist }
96-
it { is_expected.to contain(/require 'rails_helper'/) }
97-
it { is_expected.to contain(/^RSpec.describe "posts\/index.html.erb", #{type_metatag(:view)}/) }
111+
subject(:filename) { file('spec/views/posts/index.html.erb_spec.rb') }
112+
113+
it 'includes the standard boilerplate' do
114+
expect(filename).to contain(/require 'rails_helper'/)
115+
.and(contain(/^RSpec.describe "posts\/index.html.erb", #{type_metatag(:view)}/))
116+
end
98117
end
118+
99119
describe 'show.html.erb' do
100-
subject { file('spec/views/posts/show.html.erb_spec.rb') }
101-
it { is_expected.to exist }
102-
it { is_expected.to contain(/require 'rails_helper'/) }
103-
it { is_expected.to contain(/^RSpec.describe "posts\/show.html.erb", #{type_metatag(:view)}/) }
120+
subject(:filename) { file('spec/views/posts/show.html.erb_spec.rb') }
121+
122+
it 'includes the standard boilerplate' do
123+
expect(filename).to contain(/require 'rails_helper'/)
124+
.and(contain(/^RSpec.describe "posts\/show.html.erb", #{type_metatag(:view)}/))
125+
end
104126
end
105127
end
128+
106129
describe 'with haml' do
107130
before do
108131
run_generator %w[posts index --template_engine haml]
109132
end
133+
110134
describe 'index.html.haml' do
111-
subject { file('spec/views/posts/index.html.haml_spec.rb') }
112-
it { is_expected.to exist }
113-
it { is_expected.to contain(/require 'rails_helper'/) }
114-
it { is_expected.to contain(/^RSpec.describe "posts\/index.html.haml", #{type_metatag(:view)}/) }
135+
subject(:filename) { file('spec/views/posts/index.html.haml_spec.rb') }
136+
137+
it 'includes the standard boilerplate' do
138+
expect(filename).to contain(/require 'rails_helper'/)
139+
.and(contain(/^RSpec.describe "posts\/index.html.haml", #{type_metatag(:view)}/))
140+
end
115141
end
116142
end
117143
end
118144

119145
describe 'are removed' do
120-
subject { run_generator %w[posts], behavior: :revoke }
121-
it { is_expected.to match('remove spec/views/posts') }
146+
subject(:output) { run_generator %w[posts], behavior: :revoke }
147+
148+
it 'will remove the file' do
149+
expect(output).to match('remove spec/views/posts')
150+
end
122151
end
123152
end
124153

125154
describe 'routing spec' do
126-
subject { file('spec/routing/posts_routing_spec.rb') }
155+
subject(:filename) { file('spec/routing/posts_routing_spec.rb') }
127156

128157
describe 'with no flag' do
129158
before do
130159
run_generator %w[posts seek and destroy]
131160
end
132-
it { is_expected.not_to exist }
161+
162+
it 'skips the file' do
163+
expect(File.exist?(filename)).to be false
164+
end
133165
end
134166

135167
describe 'with --routing-specs flag' do
136168
describe 'without action parameter' do
137169
before do
138170
run_generator %w[posts --routing-specs]
139171
end
140-
it { is_expected.not_to exist }
172+
173+
it 'skips the file' do
174+
expect(File.exist?(filename)).to be false
175+
end
141176
end
142177

143178
describe 'with action parameter' do
144179
before { run_generator %w[posts seek --routing-specs] }
145180

146-
it { is_expected.to contain(/require 'rails_helper'/) }
147-
it { is_expected.to contain(/^RSpec.describe 'PostsController', #{type_metatag(:routing)}/) }
148-
it { is_expected.to contain(/describe 'routing'/) }
149-
it { is_expected.to contain(/it 'routes to #seek'/) }
150-
it { is_expected.to contain(/expect\(get: "\/posts\/seek"\).to route_to\("posts#seek"\)/) }
181+
it 'includes the standard boilerplate' do
182+
expect(filename).to contain(/require 'rails_helper'/)
183+
.and(contain(/^RSpec.describe 'PostsController', #{type_metatag(:routing)}/))
184+
.and(contain(/describe 'routing'/))
185+
.and(contain(/it 'routes to #seek'/))
186+
.and(contain(/expect\(get: "\/posts\/seek"\).to route_to\("posts#seek"\)/))
187+
end
151188
end
152189
end
153190

154191
describe 'with --no-routing-specs flag' do
155192
before do
156193
run_generator %w[posts seek and destroy --no-routing_specs]
157194
end
158-
it { is_expected.not_to exist }
195+
196+
it 'skips the file' do
197+
expect(File.exist?(filename)).to be false
198+
end
159199
end
160200
end
161201

162202
describe 'controller specs' do
163-
subject { file('spec/controllers/posts_controller_spec.rb') }
203+
subject(:filename) { file('spec/controllers/posts_controller_spec.rb') }
164204

165-
describe 'are not generated' do
166-
it { is_expected.not_to exist }
205+
it 'are not generated' do
206+
expect(File.exist?(filename)).to be false
167207
end
168208

169209
describe 'with --controller-specs flag' do
@@ -172,17 +212,21 @@
172212
end
173213

174214
describe 'the spec' do
175-
it { is_expected.to exist }
176-
it { is_expected.to contain(/require 'rails_helper'/) }
177-
it { is_expected.to contain(/^RSpec.describe PostsController, #{type_metatag(:controller)}/) }
215+
it 'includes the standard boilerplate' do
216+
expect(filename).to contain(/require 'rails_helper'/)
217+
.and(contain(/^RSpec.describe PostsController, #{type_metatag(:controller)}/))
218+
end
178219
end
179220
end
180221

181222
describe 'with --no-controller_specs flag' do
182223
before do
183224
run_generator %w[posts --no-controller-specs]
184225
end
185-
it { is_expected.not_to exist }
226+
227+
it 'are skipped' do
228+
expect(File.exist?(filename)).to be false
229+
end
186230
end
187231
end
188232
end

spec/generators/rspec/feature/feature_generator_spec.rb

+14-16
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,14 @@
1010
before do
1111
run_generator %w[posts]
1212
end
13+
1314
describe 'the spec' do
1415
subject(:feature_spec) { file('spec/features/posts_spec.rb') }
15-
it "exists" do
16-
expect(feature_spec).to exist
17-
end
18-
it "contains 'rails_helper'" do
19-
expect(feature_spec).to contain(/require 'rails_helper'/)
20-
end
21-
it "contains the feature" do
22-
expect(feature_spec).to contain(/^RSpec.feature "Posts", #{type_metatag(:feature)}/)
16+
17+
it 'includes the standard boilerplate' do
18+
expect(
19+
feature_spec
20+
).to contain(/require 'rails_helper'/).and(contain(/^RSpec.feature "Posts", #{type_metatag(:feature)}/))
2321
end
2422
end
2523
end
@@ -28,12 +26,11 @@
2826
before do
2927
run_generator %w[folder/posts]
3028
end
29+
3130
describe 'the spec' do
3231
subject(:feature_spec) { file('spec/features/folder/posts_spec.rb') }
33-
it "exists" do
34-
expect(feature_spec).to exist
35-
end
36-
it "contains the feature" do
32+
33+
it 'includes the standard boilerplate' do
3734
expect(feature_spec).to contain(/^RSpec.feature "Folder::Posts", #{type_metatag(:feature)}/)
3835
end
3936
end
@@ -43,11 +40,10 @@
4340
before do
4441
run_generator %w[posts --singularize]
4542
end
43+
4644
describe 'the spec' do
4745
subject(:feature_spec) { file('spec/features/post_spec.rb') }
48-
it "exists with the appropriate filename" do
49-
expect(feature_spec).to exist
50-
end
46+
5147
it "contains the singularized feature" do
5248
expect(feature_spec).to contain(/^RSpec.feature "Post", #{type_metatag(:feature)}/)
5349
end
@@ -58,10 +54,12 @@
5854
before do
5955
run_generator %w[posts --no-feature-specs]
6056
end
57+
6158
describe "the spec" do
6259
subject(:feature_spec) { file('spec/features/posts_spec.rb') }
60+
6361
it "does not exist" do
64-
expect(feature_spec).to_not exist
62+
expect(File.exist?(feature_spec)).to be false
6563
end
6664
end
6765
end

spec/generators/rspec/generator/generator_generator_spec.rb

+3-8
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,9 @@
99
before do
1010
run_generator %w[posts]
1111
end
12-
it "creates the spec file by default" do
13-
expect(generator_spec).to exist
14-
end
15-
it "contains 'rails_helper in the spec file'" do
16-
expect(generator_spec).to contain(/require 'rails_helper'/)
17-
end
18-
it "includes the generator type in the metadata" do
19-
expect(generator_spec).to contain(/^RSpec.describe "Posts", #{type_metatag(:generator)}/)
12+
13+
it "include the standard boilerplate" do
14+
expect(generator_spec).to contain(/require 'rails_helper'/).and(contain(/^RSpec.describe "Posts", #{type_metatag(:generator)}/))
2015
end
2116
end
2217
end

0 commit comments

Comments
 (0)