Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix scaffold template with namespace and --model-name option #2534

Merged
merged 6 commits into from Dec 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 13 additions & 13 deletions lib/generators/rspec/scaffold/templates/api_controller_spec.rb
Expand Up @@ -64,21 +64,21 @@
context "with valid params" do
it "creates a new <%= class_name %>" do
expect {
post :create, params: {<%= ns_file_name %>: valid_attributes}, session: valid_session
post :create, params: {<%= singular_table_name %>: valid_attributes}, session: valid_session
}.to change(<%= class_name %>, :count).by(1)
end

it "renders a JSON response with the new <%= ns_file_name %>" do
post :create, params: {<%= ns_file_name %>: valid_attributes}, session: valid_session
it "renders a JSON response with the new <%= singular_table_name %>" do
post :create, params: {<%= singular_table_name %>: valid_attributes}, session: valid_session
expect(response).to have_http_status(:created)
expect(response.content_type).to eq('application/json')
expect(response.location).to eq(<%= ns_file_name %>_url(<%= class_name %>.last))
expect(response.location).to eq(<%= singular_table_name %>_url(<%= class_name %>.last))
end
end

context "with invalid params" do
it "renders a JSON response with errors for the new <%= ns_file_name %>" do
post :create, params: {<%= ns_file_name %>: invalid_attributes}, session: valid_session
it "renders a JSON response with errors for the new <%= singular_table_name %>" do
post :create, params: {<%= singular_table_name %>: invalid_attributes}, session: valid_session
expect(response).to have_http_status(:unprocessable_entity)
expect(response.content_type).to eq('application/json')
end
Expand All @@ -91,33 +91,33 @@
skip("Add a hash of attributes valid for your model")
}

it "updates the requested <%= ns_file_name %>" do
it "updates the requested <%= singular_table_name %>" do
<%= file_name %> = <%= class_name %>.create! valid_attributes
put :update, params: {id: <%= file_name %>.to_param, <%= ns_file_name %>: new_attributes}, session: valid_session
put :update, params: {id: <%= file_name %>.to_param, <%= singular_table_name %>: new_attributes}, session: valid_session
<%= file_name %>.reload
skip("Add assertions for updated state")
end

it "renders a JSON response with the <%= ns_file_name %>" do
it "renders a JSON response with the <%= singular_table_name %>" do
<%= file_name %> = <%= class_name %>.create! valid_attributes
put :update, params: {id: <%= file_name %>.to_param, <%= ns_file_name %>: valid_attributes}, session: valid_session
put :update, params: {id: <%= file_name %>.to_param, <%= singular_table_name %>: valid_attributes}, session: valid_session
expect(response).to have_http_status(:ok)
expect(response.content_type).to eq('application/json')
end
end

context "with invalid params" do
it "renders a JSON response with errors for the <%= ns_file_name %>" do
it "renders a JSON response with errors for the <%= singular_table_name %>" do
<%= file_name %> = <%= class_name %>.create! valid_attributes
put :update, params: {id: <%= file_name %>.to_param, <%= ns_file_name %>: invalid_attributes}, session: valid_session
put :update, params: {id: <%= file_name %>.to_param, <%= singular_table_name %>: invalid_attributes}, session: valid_session
expect(response).to have_http_status(:unprocessable_entity)
expect(response.content_type).to eq('application/json')
end
end
end

describe "DELETE #destroy" do
it "destroys the requested <%= ns_file_name %>" do
it "destroys the requested <%= singular_table_name %>" do
<%= file_name %> = <%= class_name %>.create! valid_attributes
expect {
delete :destroy, params: {id: <%= file_name %>.to_param}, session: valid_session
Expand Down
20 changes: 10 additions & 10 deletions lib/generators/rspec/scaffold/templates/api_request_spec.rb
Expand Up @@ -56,13 +56,13 @@
it "creates a new <%= class_name %>" do
expect {
post <%= index_helper %>_url,
params: { <%= ns_file_name %>: valid_attributes }, headers: valid_headers, as: :json
params: { <%= singular_table_name %>: valid_attributes }, headers: valid_headers, as: :json
}.to change(<%= class_name %>, :count).by(1)
end

it "renders a JSON response with the new <%= ns_file_name %>" do
it "renders a JSON response with the new <%= singular_table_name %>" do
post <%= index_helper %>_url,
params: { <%= ns_file_name %>: valid_attributes }, headers: valid_headers, as: :json
params: { <%= singular_table_name %>: valid_attributes }, headers: valid_headers, as: :json
expect(response).to have_http_status(:created)
expect(response.content_type).to match(a_string_including("application/json"))
end
Expand All @@ -72,13 +72,13 @@
it "does not create a new <%= class_name %>" do
expect {
post <%= index_helper %>_url,
params: { <%= ns_file_name %>: invalid_attributes }, as: :json
params: { <%= singular_table_name %>: invalid_attributes }, as: :json
}.to change(<%= class_name %>, :count).by(0)
end

it "renders a JSON response with errors for the new <%= ns_file_name %>" do
it "renders a JSON response with errors for the new <%= singular_table_name %>" do
post <%= index_helper %>_url,
params: { <%= ns_file_name %>: invalid_attributes }, headers: valid_headers, as: :json
params: { <%= singular_table_name %>: invalid_attributes }, headers: valid_headers, as: :json
expect(response).to have_http_status(:unprocessable_entity)
expect(response.content_type).to match(a_string_including("application/json"))
end
Expand All @@ -91,15 +91,15 @@
skip("Add a hash of attributes valid for your model")
}

it "updates the requested <%= ns_file_name %>" do
it "updates the requested <%= singular_table_name %>" do
<%= file_name %> = <%= class_name %>.create! valid_attributes
patch <%= show_helper %>,
params: { <%= singular_table_name %>: new_attributes }, headers: valid_headers, as: :json
<%= file_name %>.reload
skip("Add assertions for updated state")
end

it "renders a JSON response with the <%= ns_file_name %>" do
it "renders a JSON response with the <%= singular_table_name %>" do
<%= file_name %> = <%= class_name %>.create! valid_attributes
patch <%= show_helper %>,
params: { <%= singular_table_name %>: new_attributes }, headers: valid_headers, as: :json
Expand All @@ -109,7 +109,7 @@
end

context "with invalid parameters" do
it "renders a JSON response with errors for the <%= ns_file_name %>" do
it "renders a JSON response with errors for the <%= singular_table_name %>" do
<%= file_name %> = <%= class_name %>.create! valid_attributes
patch <%= show_helper %>,
params: { <%= singular_table_name %>: invalid_attributes }, headers: valid_headers, as: :json
Expand All @@ -120,7 +120,7 @@
end

describe "DELETE /destroy" do
it "destroys the requested <%= ns_file_name %>" do
it "destroys the requested <%= singular_table_name %>" do
<%= file_name %> = <%= class_name %>.create! valid_attributes
expect {
delete <%= show_helper %>, headers: valid_headers, as: :json
Expand Down
20 changes: 10 additions & 10 deletions lib/generators/rspec/scaffold/templates/controller_spec.rb
Expand Up @@ -79,19 +79,19 @@
context "with valid params" do
it "creates a new <%= class_name %>" do
expect {
post :create, params: {<%= ns_file_name %>: valid_attributes}, session: valid_session
post :create, params: {<%= singular_table_name %>: valid_attributes}, session: valid_session
}.to change(<%= class_name %>, :count).by(1)
end

it "redirects to the created <%= ns_file_name %>" do
post :create, params: {<%= ns_file_name %>: valid_attributes}, session: valid_session
it "redirects to the created <%= singular_table_name %>" do
post :create, params: {<%= singular_table_name %>: valid_attributes}, session: valid_session
expect(response).to redirect_to(<%= class_name %>.last)
end
end

context "with invalid params" do
it "returns a success response (i.e. to display the 'new' template)" do
post :create, params: {<%= ns_file_name %>: invalid_attributes}, session: valid_session
post :create, params: {<%= singular_table_name %>: invalid_attributes}, session: valid_session
expect(response).to be_successful
end
end
Expand All @@ -103,31 +103,31 @@
skip("Add a hash of attributes valid for your model")
}

it "updates the requested <%= ns_file_name %>" do
it "updates the requested <%= singular_table_name %>" do
<%= file_name %> = <%= class_name %>.create! valid_attributes
put :update, params: {id: <%= file_name %>.to_param, <%= ns_file_name %>: new_attributes}, session: valid_session
put :update, params: {id: <%= file_name %>.to_param, <%= singular_table_name %>: new_attributes}, session: valid_session
<%= file_name %>.reload
skip("Add assertions for updated state")
end

it "redirects to the <%= ns_file_name %>" do
it "redirects to the <%= singular_table_name %>" do
<%= file_name %> = <%= class_name %>.create! valid_attributes
put :update, params: {id: <%= file_name %>.to_param, <%= ns_file_name %>: valid_attributes}, session: valid_session
put :update, params: {id: <%= file_name %>.to_param, <%= singular_table_name %>: valid_attributes}, session: valid_session
expect(response).to redirect_to(<%= file_name %>)
end
end

context "with invalid params" do
it "returns a success response (i.e. to display the 'edit' template)" do
<%= file_name %> = <%= class_name %>.create! valid_attributes
put :update, params: {id: <%= file_name %>.to_param, <%= ns_file_name %>: invalid_attributes}, session: valid_session
put :update, params: {id: <%= file_name %>.to_param, <%= singular_table_name %>: invalid_attributes}, session: valid_session
expect(response).to be_successful
end
end
end

describe "DELETE #destroy" do
it "destroys the requested <%= ns_file_name %>" do
it "destroys the requested <%= singular_table_name %>" do
<%= file_name %> = <%= class_name %>.create! valid_attributes
expect {
delete :destroy, params: {id: <%= file_name %>.to_param}, session: valid_session
Expand Down
16 changes: 8 additions & 8 deletions lib/generators/rspec/scaffold/templates/request_spec.rb
Expand Up @@ -65,25 +65,25 @@
context "with valid parameters" do
it "creates a new <%= class_name %>" do
expect {
post <%= index_helper %>_url, params: { <%= ns_file_name %>: valid_attributes }
post <%= index_helper %>_url, params: { <%= singular_table_name %>: valid_attributes }
}.to change(<%= class_name %>, :count).by(1)
end

it "redirects to the created <%= ns_file_name %>" do
post <%= index_helper %>_url, params: { <%= ns_file_name %>: valid_attributes }
it "redirects to the created <%= singular_table_name %>" do
post <%= index_helper %>_url, params: { <%= singular_table_name %>: valid_attributes }
expect(response).to redirect_to(<%= show_helper(class_name+".last") %>)
end
end

context "with invalid parameters" do
it "does not create a new <%= class_name %>" do
expect {
post <%= index_helper %>_url, params: { <%= ns_file_name %>: invalid_attributes }
post <%= index_helper %>_url, params: { <%= singular_table_name %>: invalid_attributes }
}.to change(<%= class_name %>, :count).by(0)
end

it "renders a successful response (i.e. to display the 'new' template)" do
post <%= index_helper %>_url, params: { <%= ns_file_name %>: invalid_attributes }
post <%= index_helper %>_url, params: { <%= singular_table_name %>: invalid_attributes }
expect(response).to be_successful
end
end
Expand All @@ -95,14 +95,14 @@
skip("Add a hash of attributes valid for your model")
}

it "updates the requested <%= ns_file_name %>" do
it "updates the requested <%= singular_table_name %>" do
<%= file_name %> = <%= class_name %>.create! valid_attributes
patch <%= show_helper %>, params: { <%= singular_table_name %>: new_attributes }
<%= file_name %>.reload
skip("Add assertions for updated state")
end

it "redirects to the <%= ns_file_name %>" do
it "redirects to the <%= singular_table_name %>" do
<%= file_name %> = <%= class_name %>.create! valid_attributes
patch <%= show_helper %>, params: { <%= singular_table_name %>: new_attributes }
<%= file_name %>.reload
Expand All @@ -120,7 +120,7 @@
end

describe "DELETE /destroy" do
it "destroys the requested <%= ns_file_name %>" do
it "destroys the requested <%= singular_table_name %>" do
<%= file_name %> = <%= class_name %>.create! valid_attributes
expect {
delete <%= show_helper %>
Expand Down
67 changes: 60 additions & 7 deletions spec/generators/rspec/scaffold/scaffold_generator_spec.rb
Expand Up @@ -101,17 +101,70 @@

describe 'namespaced request spec' do
subject { file('spec/requests/admin/posts_spec.rb') }
before { run_generator %w[admin/posts] }
it { is_expected.to exist }
it { is_expected.to contain(/^RSpec.describe "\/admin\/posts", #{type_metatag(:request)}/) }
it { is_expected.to contain('admin_post_url(post)') }
it { is_expected.to contain('Admin::Post.create') }

describe 'with default options' do
before { run_generator %w[admin/posts] }
it { is_expected.to exist }
it { is_expected.to contain(/^RSpec.describe "\/admin\/posts", #{type_metatag(:request)}/) }
it { is_expected.to contain('post admin_posts_url, params: { admin_post: valid_attributes }') }
it { is_expected.to contain('admin_post_url(post)') }
it { is_expected.to contain('Admin::Post.create') }
end

describe 'with --model-name' do
before { run_generator %w[admin/posts --model-name=post] }
it { is_expected.to contain('post admin_posts_url, params: { post: valid_attributes }') }
it { is_expected.not_to contain('params: { admin_post: valid_attributes }') }
it { is_expected.to contain(' Post.create') }
end

context 'with --api' do
describe 'with default options' do
before { run_generator %w[admin/posts --api] }
it { is_expected.to contain('params: { admin_post: valid_attributes }') }
it { is_expected.to contain('Admin::Post.create') }
end

describe 'with --model-name' do
before { run_generator %w[admin/posts --api --model-name=post] }
it { is_expected.to contain('params: { post: valid_attributes }') }
it { is_expected.not_to contain('params: { admin_post: valid_attributes }') }
it { is_expected.to contain(' Post.create') }
end
end
end

describe 'namespaced controller spec' do
subject { file('spec/controllers/admin/posts_controller_spec.rb') }
before { run_generator %w[admin/posts --controller_specs] }
it { is_expected.to contain(/^RSpec.describe Admin::PostsController, #{type_metatag(:controller)}/) }

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

describe 'with --model-name' do
before { run_generator %w[admin/posts --model-name=post --controller_specs] }
it { is_expected.to contain('post :create, params: {post: valid_attributes}') }
it { is_expected.not_to contain('params: {admin_post: valid_attributes}') }
it { is_expected.to contain(' Post.create') }
end

context 'with --api' do
describe 'with default options' do
before { run_generator %w[admin/posts --api --controller_specs] }
it { is_expected.to contain('post :create, params: {admin_post: valid_attributes}') }
it { is_expected.to contain('Admin::Post.create') }
end

describe 'with --model-name' do
before { run_generator %w[admin/posts --api --model-name=post --controller_specs] }
it { is_expected.to contain('post :create, params: {post: valid_attributes}') }
it { is_expected.not_to contain('params: {admin_post: valid_attributes}') }
it { is_expected.to contain(' Post.create') }
end
end
end

describe 'view specs' do
Expand Down