Skip to content

Commit 71a1a91

Browse files
koheisgJonRowe
authored andcommitted
Fix generate request spec name has / (#2057)
Use `name.underscore.pluralize` instead of file and table name to handle the case where generating request specs for name spaced files
1 parent c7d9f86 commit 71a1a91

File tree

3 files changed

+53
-20
lines changed

3 files changed

+53
-20
lines changed

lib/generators/rspec/integration/integration_generator.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def generate_request_spec
1515
return unless options[:request_specs]
1616

1717
template 'request_spec.rb',
18-
File.join('spec/requests', class_path, "#{table_name}_spec.rb")
18+
File.join('spec/requests', "#{name.underscore.pluralize}_spec.rb")
1919
end
2020
end
2121
end

lib/generators/rspec/integration/templates/request_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require 'rails_helper'
22

33
RSpec.describe "<%= class_name.pluralize %>", <%= type_metatag(:request) %> do
4-
describe "GET /<%= table_name %>" do
4+
describe "GET /<%= name.underscore.pluralize %>" do
55
it "works! (now write some real specs)" do
66
get <%= index_helper %>_path
77
expect(response).to have_http_status(200)

spec/support/generators.rb

Lines changed: 51 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,32 +34,65 @@ def self.included(klass)
3434

3535
describe 'generated with no flags' do
3636
before do
37-
run_generator %w(posts)
37+
run_generator name
3838
end
3939

40-
subject(:request_spec) { file('spec/requests/posts_spec.rb') }
40+
subject(:request_spec) { file(spec_file_name) }
4141

42-
it "creates the request spec" do
43-
expect(request_spec).to exist
44-
end
42+
context 'When NAME=posts' do
43+
let(:name) { %w(posts) }
44+
let(:spec_file_name) { 'spec/requests/posts_spec.rb' }
4545

46-
it "the generator requires 'rails_helper'" do
47-
expect(request_spec).to contain(/require 'rails_helper'/)
48-
end
46+
it "creates the request spec" do
47+
expect(request_spec).to exist
48+
end
4949

50-
it "the generator describes the provided NAME without monkey " \
51-
"patching setting the type to `:request`" do
52-
expect(request_spec).to contain(
53-
/^RSpec.describe \"Posts\", #{type_metatag(:request)}/
54-
)
55-
end
50+
it "the generator requires 'rails_helper'" do
51+
expect(request_spec).to contain(/require 'rails_helper'/)
52+
end
53+
54+
it "the generator describes the provided NAME without monkey " \
55+
"patching setting the type to `:request`" do
56+
expect(request_spec).to contain(
57+
/^RSpec.describe \"Posts\", #{type_metatag(:request)}/
58+
)
59+
end
60+
61+
it "the generator includes a sample GET request" do
62+
expect(request_spec).to contain(/describe "GET \/posts"/)
63+
end
5664

57-
it "the generator includes a sample GET request" do
58-
expect(request_spec).to contain(/describe "GET \/posts"/)
65+
it "the generator sends the GET request to the index path" do
66+
expect(request_spec).to contain(/get posts_index_path/)
67+
end
5968
end
6069

61-
it "the generator sends the GET request to the index path" do
62-
expect(request_spec).to contain(/get posts_index_path/)
70+
context 'When NAME=api/posts' do
71+
let(:name) { %w(api/posts) }
72+
let(:spec_file_name) { 'spec/requests/api/posts_spec.rb' }
73+
74+
it "creates the request spec" do
75+
expect(request_spec).to exist
76+
end
77+
78+
it "the generator requires 'rails_helper'" do
79+
expect(request_spec).to contain(/require 'rails_helper'/)
80+
end
81+
82+
it "the generator describes the provided NAME without monkey " \
83+
"patching setting the type to `:request`" do
84+
expect(request_spec).to contain(
85+
/^RSpec.describe \"Api::Posts\", #{type_metatag(:request)}/
86+
)
87+
end
88+
89+
it "the generator includes a sample GET request" do
90+
expect(request_spec).to contain(/describe "GET \/api\/posts\"/)
91+
end
92+
93+
it "the generator sends the GET request to the index path" do
94+
expect(request_spec).to contain(/get api_posts_index_path\n/)
95+
end
6396
end
6497
end
6598
end

0 commit comments

Comments
 (0)