Skip to content

Commit 60b7a7e

Browse files
committed
Merge pull request #2534 from kenzo-tanaka/fix-scaffold-request-template
Fix scaffold template with namespace and `--model-name` option
1 parent 69426ef commit 60b7a7e

File tree

5 files changed

+101
-48
lines changed

5 files changed

+101
-48
lines changed

lib/generators/rspec/scaffold/templates/api_controller_spec.rb

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -64,21 +64,21 @@
6464
context "with valid params" do
6565
it "creates a new <%= class_name %>" do
6666
expect {
67-
post :create, params: {<%= ns_file_name %>: valid_attributes}, session: valid_session
67+
post :create, params: {<%= singular_table_name %>: valid_attributes}, session: valid_session
6868
}.to change(<%= class_name %>, :count).by(1)
6969
end
7070
71-
it "renders a JSON response with the new <%= ns_file_name %>" do
72-
post :create, params: {<%= ns_file_name %>: valid_attributes}, session: valid_session
71+
it "renders a JSON response with the new <%= singular_table_name %>" do
72+
post :create, params: {<%= singular_table_name %>: valid_attributes}, session: valid_session
7373
expect(response).to have_http_status(:created)
7474
expect(response.content_type).to eq('application/json')
75-
expect(response.location).to eq(<%= ns_file_name %>_url(<%= class_name %>.last))
75+
expect(response.location).to eq(<%= singular_table_name %>_url(<%= class_name %>.last))
7676
end
7777
end
7878
7979
context "with invalid params" do
80-
it "renders a JSON response with errors for the new <%= ns_file_name %>" do
81-
post :create, params: {<%= ns_file_name %>: invalid_attributes}, session: valid_session
80+
it "renders a JSON response with errors for the new <%= singular_table_name %>" do
81+
post :create, params: {<%= singular_table_name %>: invalid_attributes}, session: valid_session
8282
expect(response).to have_http_status(:unprocessable_entity)
8383
expect(response.content_type).to eq('application/json')
8484
end
@@ -91,33 +91,33 @@
9191
skip("Add a hash of attributes valid for your model")
9292
}
9393
94-
it "updates the requested <%= ns_file_name %>" do
94+
it "updates the requested <%= singular_table_name %>" do
9595
<%= file_name %> = <%= class_name %>.create! valid_attributes
96-
put :update, params: {id: <%= file_name %>.to_param, <%= ns_file_name %>: new_attributes}, session: valid_session
96+
put :update, params: {id: <%= file_name %>.to_param, <%= singular_table_name %>: new_attributes}, session: valid_session
9797
<%= file_name %>.reload
9898
skip("Add assertions for updated state")
9999
end
100100
101-
it "renders a JSON response with the <%= ns_file_name %>" do
101+
it "renders a JSON response with the <%= singular_table_name %>" do
102102
<%= file_name %> = <%= class_name %>.create! valid_attributes
103-
put :update, params: {id: <%= file_name %>.to_param, <%= ns_file_name %>: valid_attributes}, session: valid_session
103+
put :update, params: {id: <%= file_name %>.to_param, <%= singular_table_name %>: valid_attributes}, session: valid_session
104104
expect(response).to have_http_status(:ok)
105105
expect(response.content_type).to eq('application/json')
106106
end
107107
end
108108
109109
context "with invalid params" do
110-
it "renders a JSON response with errors for the <%= ns_file_name %>" do
110+
it "renders a JSON response with errors for the <%= singular_table_name %>" do
111111
<%= file_name %> = <%= class_name %>.create! valid_attributes
112-
put :update, params: {id: <%= file_name %>.to_param, <%= ns_file_name %>: invalid_attributes}, session: valid_session
112+
put :update, params: {id: <%= file_name %>.to_param, <%= singular_table_name %>: invalid_attributes}, session: valid_session
113113
expect(response).to have_http_status(:unprocessable_entity)
114114
expect(response.content_type).to eq('application/json')
115115
end
116116
end
117117
end
118118
119119
describe "DELETE #destroy" do
120-
it "destroys the requested <%= ns_file_name %>" do
120+
it "destroys the requested <%= singular_table_name %>" do
121121
<%= file_name %> = <%= class_name %>.create! valid_attributes
122122
expect {
123123
delete :destroy, params: {id: <%= file_name %>.to_param}, session: valid_session

lib/generators/rspec/scaffold/templates/api_request_spec.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,13 @@
5656
it "creates a new <%= class_name %>" do
5757
expect {
5858
post <%= index_helper %>_url,
59-
params: { <%= ns_file_name %>: valid_attributes }, headers: valid_headers, as: :json
59+
params: { <%= singular_table_name %>: valid_attributes }, headers: valid_headers, as: :json
6060
}.to change(<%= class_name %>, :count).by(1)
6161
end
6262
63-
it "renders a JSON response with the new <%= ns_file_name %>" do
63+
it "renders a JSON response with the new <%= singular_table_name %>" do
6464
post <%= index_helper %>_url,
65-
params: { <%= ns_file_name %>: valid_attributes }, headers: valid_headers, as: :json
65+
params: { <%= singular_table_name %>: valid_attributes }, headers: valid_headers, as: :json
6666
expect(response).to have_http_status(:created)
6767
expect(response.content_type).to match(a_string_including("application/json"))
6868
end
@@ -72,13 +72,13 @@
7272
it "does not create a new <%= class_name %>" do
7373
expect {
7474
post <%= index_helper %>_url,
75-
params: { <%= ns_file_name %>: invalid_attributes }, as: :json
75+
params: { <%= singular_table_name %>: invalid_attributes }, as: :json
7676
}.to change(<%= class_name %>, :count).by(0)
7777
end
7878
79-
it "renders a JSON response with errors for the new <%= ns_file_name %>" do
79+
it "renders a JSON response with errors for the new <%= singular_table_name %>" do
8080
post <%= index_helper %>_url,
81-
params: { <%= ns_file_name %>: invalid_attributes }, headers: valid_headers, as: :json
81+
params: { <%= singular_table_name %>: invalid_attributes }, headers: valid_headers, as: :json
8282
expect(response).to have_http_status(:unprocessable_entity)
8383
expect(response.content_type).to eq("application/json")
8484
end
@@ -91,15 +91,15 @@
9191
skip("Add a hash of attributes valid for your model")
9292
}
9393
94-
it "updates the requested <%= ns_file_name %>" do
94+
it "updates the requested <%= singular_table_name %>" do
9595
<%= file_name %> = <%= class_name %>.create! valid_attributes
9696
patch <%= show_helper.tr('@', '') %>,
9797
params: { <%= singular_table_name %>: new_attributes }, headers: valid_headers, as: :json
9898
<%= file_name %>.reload
9999
skip("Add assertions for updated state")
100100
end
101101
102-
it "renders a JSON response with the <%= ns_file_name %>" do
102+
it "renders a JSON response with the <%= singular_table_name %>" do
103103
<%= file_name %> = <%= class_name %>.create! valid_attributes
104104
patch <%= show_helper.tr('@', '') %>,
105105
params: { <%= singular_table_name %>: new_attributes }, headers: valid_headers, as: :json
@@ -109,7 +109,7 @@
109109
end
110110
111111
context "with invalid parameters" do
112-
it "renders a JSON response with errors for the <%= ns_file_name %>" do
112+
it "renders a JSON response with errors for the <%= singular_table_name %>" do
113113
<%= file_name %> = <%= class_name %>.create! valid_attributes
114114
patch <%= show_helper.tr('@', '') %>,
115115
params: { <%= singular_table_name %>: invalid_attributes }, headers: valid_headers, as: :json
@@ -120,7 +120,7 @@
120120
end
121121
122122
describe "DELETE /destroy" do
123-
it "destroys the requested <%= ns_file_name %>" do
123+
it "destroys the requested <%= singular_table_name %>" do
124124
<%= file_name %> = <%= class_name %>.create! valid_attributes
125125
expect {
126126
delete <%= show_helper.tr('@', '') %>, headers: valid_headers, as: :json

lib/generators/rspec/scaffold/templates/controller_spec.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,19 +79,19 @@
7979
context "with valid params" do
8080
it "creates a new <%= class_name %>" do
8181
expect {
82-
post :create, params: {<%= ns_file_name %>: valid_attributes}, session: valid_session
82+
post :create, params: {<%= singular_table_name %>: valid_attributes}, session: valid_session
8383
}.to change(<%= class_name %>, :count).by(1)
8484
end
8585
86-
it "redirects to the created <%= ns_file_name %>" do
87-
post :create, params: {<%= ns_file_name %>: valid_attributes}, session: valid_session
86+
it "redirects to the created <%= singular_table_name %>" do
87+
post :create, params: {<%= singular_table_name %>: valid_attributes}, session: valid_session
8888
expect(response).to redirect_to(<%= class_name %>.last)
8989
end
9090
end
9191
9292
context "with invalid params" do
9393
it "returns a success response (i.e. to display the 'new' template)" do
94-
post :create, params: {<%= ns_file_name %>: invalid_attributes}, session: valid_session
94+
post :create, params: {<%= singular_table_name %>: invalid_attributes}, session: valid_session
9595
expect(response).to be_successful
9696
end
9797
end
@@ -103,31 +103,31 @@
103103
skip("Add a hash of attributes valid for your model")
104104
}
105105
106-
it "updates the requested <%= ns_file_name %>" do
106+
it "updates the requested <%= singular_table_name %>" do
107107
<%= file_name %> = <%= class_name %>.create! valid_attributes
108-
put :update, params: {id: <%= file_name %>.to_param, <%= ns_file_name %>: new_attributes}, session: valid_session
108+
put :update, params: {id: <%= file_name %>.to_param, <%= singular_table_name %>: new_attributes}, session: valid_session
109109
<%= file_name %>.reload
110110
skip("Add assertions for updated state")
111111
end
112112
113-
it "redirects to the <%= ns_file_name %>" do
113+
it "redirects to the <%= singular_table_name %>" do
114114
<%= file_name %> = <%= class_name %>.create! valid_attributes
115-
put :update, params: {id: <%= file_name %>.to_param, <%= ns_file_name %>: valid_attributes}, session: valid_session
115+
put :update, params: {id: <%= file_name %>.to_param, <%= singular_table_name %>: valid_attributes}, session: valid_session
116116
expect(response).to redirect_to(<%= file_name %>)
117117
end
118118
end
119119
120120
context "with invalid params" do
121121
it "returns a success response (i.e. to display the 'edit' template)" do
122122
<%= file_name %> = <%= class_name %>.create! valid_attributes
123-
put :update, params: {id: <%= file_name %>.to_param, <%= ns_file_name %>: invalid_attributes}, session: valid_session
123+
put :update, params: {id: <%= file_name %>.to_param, <%= singular_table_name %>: invalid_attributes}, session: valid_session
124124
expect(response).to be_successful
125125
end
126126
end
127127
end
128128
129129
describe "DELETE #destroy" do
130-
it "destroys the requested <%= ns_file_name %>" do
130+
it "destroys the requested <%= singular_table_name %>" do
131131
<%= file_name %> = <%= class_name %>.create! valid_attributes
132132
expect {
133133
delete :destroy, params: {id: <%= file_name %>.to_param}, session: valid_session

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,25 +65,25 @@
6565
context "with valid parameters" do
6666
it "creates a new <%= class_name %>" do
6767
expect {
68-
post <%= index_helper %>_url, params: { <%= ns_file_name %>: valid_attributes }
68+
post <%= index_helper %>_url, params: { <%= singular_table_name %>: valid_attributes }
6969
}.to change(<%= class_name %>, :count).by(1)
7070
end
7171
72-
it "redirects to the created <%= ns_file_name %>" do
73-
post <%= index_helper %>_url, params: { <%= ns_file_name %>: valid_attributes }
72+
it "redirects to the created <%= singular_table_name %>" do
73+
post <%= index_helper %>_url, params: { <%= singular_table_name %>: valid_attributes }
7474
expect(response).to redirect_to(<%= show_helper.gsub("\@#{file_name}", class_name+".last") %>)
7575
end
7676
end
7777
7878
context "with invalid parameters" do
7979
it "does not create a new <%= class_name %>" do
8080
expect {
81-
post <%= index_helper %>_url, params: { <%= ns_file_name %>: invalid_attributes }
81+
post <%= index_helper %>_url, params: { <%= singular_table_name %>: invalid_attributes }
8282
}.to change(<%= class_name %>, :count).by(0)
8383
end
8484
8585
it "renders a successful response (i.e. to display the 'new' template)" do
86-
post <%= index_helper %>_url, params: { <%= ns_file_name %>: invalid_attributes }
86+
post <%= index_helper %>_url, params: { <%= singular_table_name %>: invalid_attributes }
8787
expect(response).to be_successful
8888
end
8989
end
@@ -95,14 +95,14 @@
9595
skip("Add a hash of attributes valid for your model")
9696
}
9797
98-
it "updates the requested <%= ns_file_name %>" do
98+
it "updates the requested <%= singular_table_name %>" do
9999
<%= file_name %> = <%= class_name %>.create! valid_attributes
100100
patch <%= show_helper.tr('@', '') %>, params: { <%= singular_table_name %>: new_attributes }
101101
<%= file_name %>.reload
102102
skip("Add assertions for updated state")
103103
end
104104
105-
it "redirects to the <%= ns_file_name %>" do
105+
it "redirects to the <%= singular_table_name %>" do
106106
<%= file_name %> = <%= class_name %>.create! valid_attributes
107107
patch <%= show_helper.tr('@', '') %>, params: { <%= singular_table_name %>: new_attributes }
108108
<%= file_name %>.reload
@@ -120,7 +120,7 @@
120120
end
121121
122122
describe "DELETE /destroy" do
123-
it "destroys the requested <%= ns_file_name %>" do
123+
it "destroys the requested <%= singular_table_name %>" do
124124
<%= file_name %> = <%= class_name %>.create! valid_attributes
125125
expect {
126126
delete <%= show_helper.tr('@', '') %>

spec/generators/rspec/scaffold/scaffold_generator_spec.rb

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,17 +101,70 @@
101101

102102
describe 'namespaced request spec' do
103103
subject { file('spec/requests/admin/posts_spec.rb') }
104-
before { run_generator %w[admin/posts] }
105-
it { is_expected.to exist }
106-
it { is_expected.to contain(/^RSpec.describe "\/admin\/posts", #{type_metatag(:request)}/) }
107-
it { is_expected.to contain('admin_post_url(admin_post)') }
108-
it { is_expected.to contain('Admin::Post.create') }
104+
105+
describe 'with default options' do
106+
before { run_generator %w[admin/posts] }
107+
it { is_expected.to exist }
108+
it { is_expected.to contain(/^RSpec.describe "\/admin\/posts", #{type_metatag(:request)}/) }
109+
it { is_expected.to contain('post admin_posts_url, params: { admin_post: valid_attributes }') }
110+
it { is_expected.to contain('admin_post_url(post)') }
111+
it { is_expected.to contain('Admin::Post.create') }
112+
end
113+
114+
describe 'with --model-name' do
115+
before { run_generator %w[admin/posts --model-name=post] }
116+
it { is_expected.to contain('post admin_posts_url, params: { post: valid_attributes }') }
117+
it { is_expected.not_to contain('params: { admin_post: valid_attributes }') }
118+
it { is_expected.to contain(' Post.create') }
119+
end
120+
121+
context 'with --api' do
122+
describe 'with default options' do
123+
before { run_generator %w[admin/posts --api] }
124+
it { is_expected.to contain('params: { admin_post: valid_attributes }') }
125+
it { is_expected.to contain('Admin::Post.create') }
126+
end
127+
128+
describe 'with --model-name' do
129+
before { run_generator %w[admin/posts --api --model-name=post] }
130+
it { is_expected.to contain('params: { post: valid_attributes }') }
131+
it { is_expected.not_to contain('params: { admin_post: valid_attributes }') }
132+
it { is_expected.to contain(' Post.create') }
133+
end
134+
end
109135
end
110136

111137
describe 'namespaced controller spec' do
112138
subject { file('spec/controllers/admin/posts_controller_spec.rb') }
113-
before { run_generator %w[admin/posts --controller_specs] }
114-
it { is_expected.to contain(/^RSpec.describe Admin::PostsController, #{type_metatag(:controller)}/) }
139+
140+
describe 'with default options' do
141+
before { run_generator %w[admin/posts --controller_specs] }
142+
it { is_expected.to contain(/^RSpec.describe Admin::PostsController, #{type_metatag(:controller)}/) }
143+
it { is_expected.to contain('post :create, params: {admin_post: valid_attributes}') }
144+
it { is_expected.to contain('Admin::Post.create') }
145+
end
146+
147+
describe 'with --model-name' do
148+
before { run_generator %w[admin/posts --model-name=post --controller_specs] }
149+
it { is_expected.to contain('post :create, params: {post: valid_attributes}') }
150+
it { is_expected.not_to contain('params: {admin_post: valid_attributes}') }
151+
it { is_expected.to contain(' Post.create') }
152+
end
153+
154+
context 'with --api' do
155+
describe 'with default options' do
156+
before { run_generator %w[admin/posts --api --controller_specs] }
157+
it { is_expected.to contain('post :create, params: {admin_post: valid_attributes}') }
158+
it { is_expected.to contain('Admin::Post.create') }
159+
end
160+
161+
describe 'with --model-name' do
162+
before { run_generator %w[admin/posts --api --model-name=post --controller_specs] }
163+
it { is_expected.to contain('post :create, params: {post: valid_attributes}') }
164+
it { is_expected.not_to contain('params: {admin_post: valid_attributes}') }
165+
it { is_expected.to contain(' Post.create') }
166+
end
167+
end
115168
end
116169

117170
describe 'view specs' do

0 commit comments

Comments
 (0)