Skip to content

Commit 03f1d1a

Browse files
committed
Fix api controller template with namespace and --model-name option
1 parent c4585e2 commit 03f1d1a

File tree

2 files changed

+34
-19
lines changed

2 files changed

+34
-19
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

spec/generators/rspec/scaffold/scaffold_generator_spec.rb

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@
102102
describe 'namespaced request spec' do
103103
subject { file('spec/requests/admin/posts_spec.rb') }
104104

105-
describe 'with no options' do
106-
before { run_generator %w[admin/posts] }
105+
describe 'with default options' do
106+
before { run_generator %w[admin/posts] }
107107
it { is_expected.to exist }
108108
it { is_expected.to contain(/^RSpec.describe "\/admin\/posts", #{type_metatag(:request)}/) }
109109
it { is_expected.to contain('post admin_posts_url, params: { admin_post: valid_attributes }') }
@@ -112,7 +112,7 @@
112112
end
113113

114114
describe 'with --model-name' do
115-
before { run_generator %w[admin/posts --model-name=post] }
115+
before { run_generator %w[admin/posts --model-name=post] }
116116
it { is_expected.to contain('post admin_posts_url, params: { post: valid_attributes }') }
117117
it { is_expected.not_to contain('params: { admin_post: valid_attributes }') }
118118
it { is_expected.to contain(' Post.create') }
@@ -137,19 +137,34 @@
137137
describe 'namespaced controller spec' do
138138
subject { file('spec/controllers/admin/posts_controller_spec.rb') }
139139

140-
describe 'with no options' do
141-
before { run_generator %w[admin/posts --controller_specs] }
140+
describe 'with default options' do
141+
before { run_generator %w[admin/posts --controller_specs] }
142142
it { is_expected.to contain(/^RSpec.describe Admin::PostsController, #{type_metatag(:controller)}/) }
143143
it { is_expected.to contain('post :create, params: {admin_post: valid_attributes}') }
144144
it { is_expected.to contain('Admin::Post.create') }
145145
end
146146

147147
describe 'with --model-name' do
148-
before { run_generator %w[admin/posts --model-name=post --controller_specs] }
148+
before { run_generator %w[admin/posts --model-name=post --controller_specs] }
149149
it { is_expected.to contain('post :create, params: {post: valid_attributes}') }
150150
it { is_expected.not_to contain('params: {admin_post: valid_attributes}') }
151151
it { is_expected.to contain(' Post.create') }
152152
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
153168
end
154169

155170
describe 'view specs' do

0 commit comments

Comments
 (0)