-
-
Notifications
You must be signed in to change notification settings - Fork 1k
modified request_specs
to fit API requests.
#2046
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
Changes from all commits
c580982
a0f4790
e5587fd
0d4bebd
d345938
4775094
700bb41
b1a46c3
9cbe4d3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,12 +10,58 @@ class IntegrationGenerator < Base | |
:type => :boolean, | ||
:default => true, | ||
:desc => "Generate request specs" | ||
class_option :api, :type => :boolean, :desc => "Creates request_spec for APIs, skip specs unnecessary for API-only apps" | ||
class_option :fabrication, :type => :boolean, :desc => "Fill params with Fabricator model attributes" | ||
class_option :factorybot, :type => :boolean, :desc => "Fill params with Factory Bot model attributes" | ||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Duplicate spacing |
||
def initialize(*args, &blk) | ||
@generator_args = args.first | ||
super(*args, &blk) | ||
end | ||
|
||
def generate_request_spec | ||
return unless options[:request_specs] | ||
|
||
if options[:api] | ||
template 'api_request_spec.rb', File.join('spec/requests', class_path, "#{table_name}_spec.rb") | ||
else | ||
template 'request_spec.rb', | ||
File.join('spec/requests', class_path, "#{table_name}_spec.rb") | ||
end | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Excess whitespace and weird line formatting. |
||
end | ||
|
||
protected | ||
|
||
attr_reader :generator_args | ||
|
||
# @todo refactor the following methods. They are also in the /lib/generators/rpsec/scaffold/scaffold_generator.rb file. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Either refactor these, or remove the todo. If this PR isn't complete it can't be merged. |
||
|
||
def ns_file_name | ||
return file_name if ns_parts.empty? | ||
"#{ns_prefix.map(&:underscore).join('/')}_#{ns_suffix.singularize.underscore}" | ||
end | ||
|
||
# support for namespaced-resources | ||
def ns_table_name | ||
return table_name if ns_parts.empty? | ||
"#{ns_prefix.map(&:underscore).join('/')}/#{ns_suffix.tableize}" | ||
end | ||
|
||
def ns_parts | ||
@ns_parts ||= begin | ||
parts = generator_args[0].split(/\/|::/) | ||
parts.size > 1 ? parts : [] | ||
end | ||
end | ||
|
||
def ns_prefix | ||
@ns_prefix ||= ns_parts[0..-2] | ||
end | ||
|
||
template 'request_spec.rb', | ||
File.join('spec/requests', class_path, "#{table_name}_spec.rb") | ||
def ns_suffix | ||
@ns_suffix ||= ns_parts[-1] | ||
end | ||
end | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,59 +11,65 @@ | |
# It only uses APIs available in rails and/or rspec-rails. There are a number | ||
# of tools you can use to make these specs even more expressive, but we're | ||
# sticking to rails and rspec-rails APIs to keep things simple and stable. | ||
# | ||
# Compared to earlier versions of this generator, there is very limited use of | ||
# stubs and message expectations in this spec. Stubs are only used when there | ||
# is no simpler way to get a handle on the object needed for the example. | ||
# Message expectations are only used when there is no simpler way to specify | ||
# that an instance is receiving a specific message. | ||
# | ||
# Also compared to earlier versions of this generator, there are no longer any | ||
# expectations of assigns and templates rendered. These features have been | ||
# removed from Rails core in Rails 5, but can be added back in via the | ||
# `rails-controller-testing` gem. | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can't just remove existing functionality, if you wish to add more functionality thats fine but these files must remain as is, further comments assume you've done this and apply to a new file containing functionality. |
||
|
||
<% module_namespacing do -%> | ||
RSpec.describe <%= controller_class_name %>Controller, <%= type_metatag(:controller) %> do | ||
RSpec.describe "<%= class_name.pluralize %>", <%= type_metatag(:request) %> do | ||
|
||
# This should return the minimal set of attributes required to create a valid | ||
# <%= class_name %>. As you add validations to <%= class_name %>, be sure to | ||
# adjust the attributes here as well. | ||
let(:valid_attributes) { | ||
let(:valid_attributes) do | ||
<% if options[:factorybot] || Gem.loaded_specs.has_key?('factory_bot_rails') -%> | ||
attributes_for(:<%= ns_file_name -%>) | ||
<% elsif options[:fabrication] -%> | ||
Fabricate.attributes_for(<%= ns_prefix.empty? ? ":"+ns_file_name : "'#{class_name}'"-%>) | ||
<% else -%> | ||
skip("Add a hash of attributes valid for your model") | ||
} | ||
<% end -%> | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can remove all of the above as per the earlier comment, we don't add integration for 3rd parties here, thats for them to do. |
||
|
||
let(:invalid_attributes) { | ||
let(:invalid_attributes) do | ||
skip("Add a hash of attributes invalid for your model") | ||
} | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't arbitarily change formatting. |
||
|
||
# This should return the minimal set of values that should be in the session | ||
# This should return the minimal set of values that should be in the headers | ||
# in order to pass any filters (e.g. authentication) defined in | ||
# <%= controller_class_name %>Controller. Be sure to keep this updated too. | ||
let(:valid_session) { {} } | ||
# <%= class_name %>Controller. Be sure to keep this updated too. | ||
# Because it's an API request a default JSON header is added. | ||
# If you have an Authentication - and you definitely should - add your token header here. | ||
let(:valid_headers) do | ||
{ | ||
"Content-Type" => "application/json" | ||
} | ||
end | ||
|
||
<% unless options[:singleton] -%> | ||
describe "GET #index" do | ||
it "returns a success response" do | ||
<%= file_name %> = <%= class_name %>.create! valid_attributes | ||
<% if RUBY_VERSION < '1.9.3' -%> | ||
get :index, {}, valid_session | ||
<% else -%> | ||
get :index, params: {}, session: valid_session | ||
<% end -%> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is necessary for older Rails / Ruby, your new file will have to cater for all supported versions of Rails, this is will be easier in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can now safely be ignored, you can use rails 5 syntax. |
||
|
||
get <%= ns_file_name.pluralize %>_path, params: {}, headers: valid_headers | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When did Rails api get introduced? Just wondering if these are keyword args or a hash, if the latter should be hash rockets :/ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I dont understand what you mean... :-/ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Older versions of rails accepted a hash of options to their There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This needs resolving There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can now safely be ignored, you can use rails 5 syntax. |
||
|
||
expect(response).to be_successful | ||
end | ||
|
||
it "returns <%= class_name %> objects as json array" do | ||
<%= file_name %> = <%= class_name %>.create! valid_attributes | ||
|
||
get <%= ns_file_name.pluralize %>_path, params: {}, headers: valid_headers | ||
|
||
expect(JSON.parse(response.body).count).to be 1 | ||
end | ||
end | ||
|
||
<% end -%> | ||
describe "GET #show" do | ||
it "returns a success response" do | ||
<%= file_name %> = <%= class_name %>.create! valid_attributes | ||
<% if RUBY_VERSION < '1.9.3' -%> | ||
get :show, {:id => <%= file_name %>.to_param}, valid_session | ||
<% else -%> | ||
get :show, params: {id: <%= file_name %>.to_param}, session: valid_session | ||
<% end -%> | ||
JonRowe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
get <%= ns_file_name.pluralize %>_path, params: {id: <%= file_name %>.to_param}, headers: valid_headers | ||
|
||
expect(response).to be_successful | ||
end | ||
end | ||
|
@@ -72,20 +78,14 @@ | |
context "with valid params" do | ||
it "creates a new <%= class_name %>" do | ||
expect { | ||
<% if RUBY_VERSION < '1.9.3' -%> | ||
post :create, {:<%= ns_file_name %> => valid_attributes}, valid_session | ||
<% else -%> | ||
post :create, params: {<%= ns_file_name %>: valid_attributes}, session: valid_session | ||
<% end -%> | ||
}.to change(<%= class_name %>, :count).by(1) | ||
JonRowe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
post <%= ns_file_name.pluralize %>_path(<%= ns_file_name -%>: valid_attributes), headers: valid_headers | ||
|
||
}.to change(<%= class_name %>, :count).by(1) | ||
end | ||
|
||
it "renders a JSON response with the new <%= ns_file_name %>" do | ||
<% if RUBY_VERSION < '1.9.3' -%> | ||
post :create, {:<%= ns_file_name %> => valid_attributes}, valid_session | ||
<% else %> | ||
post :create, params: {<%= ns_file_name %>: valid_attributes}, session: valid_session | ||
<% end -%> | ||
JonRowe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
post <%= ns_file_name.pluralize %>_path(<%= ns_file_name -%>: valid_attributes), headers: valid_headers | ||
|
||
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)) | ||
|
@@ -94,11 +94,8 @@ | |
|
||
context "with invalid params" do | ||
it "renders a JSON response with errors for the new <%= ns_file_name %>" do | ||
<% if RUBY_VERSION < '1.9.3' -%> | ||
post :create, {:<%= ns_file_name %> => invalid_attributes}, valid_session | ||
<% else %> | ||
post :create, params: {<%= ns_file_name %>: invalid_attributes}, session: valid_session | ||
<% end -%> | ||
JonRowe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
post <%= ns_file_name.pluralize %>_path(<%= ns_file_name -%>: invalid_attributes), headers: valid_headers | ||
|
||
expect(response).to have_http_status(:unprocessable_entity) | ||
expect(response.content_type).to eq('application/json') | ||
end | ||
|
@@ -108,27 +105,27 @@ | |
describe "PUT #update" do | ||
context "with valid params" do | ||
let(:new_attributes) { | ||
<% if options[:factorybot] || Gem.loaded_specs.has_key?('factory_bot_rails') -%> | ||
attributes_for(:<%= ns_file_name -%>) | ||
<% elsif options[:fabrication] -%> | ||
Fabricate.attributes_for(<%= options[:skip_namespace] ? ":"+ns_file_name : "'#{ns_file_name}'"-%>) | ||
<% else -%> | ||
skip("Add a hash of attributes valid for your model") | ||
<% end -%> | ||
} | ||
|
||
it "updates the requested <%= ns_file_name %>" do | ||
<%= file_name %> = <%= class_name %>.create! valid_attributes | ||
<% if RUBY_VERSION < '1.9.3' -%> | ||
put :update, {:id => <%= file_name %>.to_param, :<%= ns_file_name %> => new_attributes}, valid_session | ||
<% else -%> | ||
put :update, params: {id: <%= file_name %>.to_param, <%= ns_file_name %>: new_attributes}, session: valid_session | ||
<% end -%> | ||
JonRowe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
put <%= ns_file_name %>_path(id: valid_attributes, <%= ns_file_name %>: new_attributes), headers: valid_headers | ||
|
||
<%= file_name %>.reload | ||
skip("Add assertions for updated state") | ||
end | ||
|
||
it "renders a JSON response with the <%= ns_file_name %>" do | ||
<%= file_name %> = <%= class_name %>.create! valid_attributes | ||
<% if RUBY_VERSION < '1.9.3' -%> | ||
put :update, {:id => <%= file_name %>.to_param, :<%= ns_file_name %> => valid_attributes}, valid_session | ||
<% else %> | ||
put :update, params: {id: <%= file_name %>.to_param, <%= ns_file_name %>: valid_attributes}, session: valid_session | ||
JonRowe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
<% end -%> | ||
put <%= ns_file_name %>_path(id: <%= file_name %>.to_param, <%= ns_file_name %>: new_attributes), headers: valid_headers | ||
|
||
expect(response).to have_http_status(:ok) | ||
expect(response.content_type).to eq('application/json') | ||
end | ||
|
@@ -137,11 +134,8 @@ | |
context "with invalid params" do | ||
it "renders a JSON response with errors for the <%= ns_file_name %>" do | ||
<%= file_name %> = <%= class_name %>.create! valid_attributes | ||
<% if RUBY_VERSION < '1.9.3' -%> | ||
put :update, {:id => <%= file_name %>.to_param, :<%= ns_file_name %> => invalid_attributes}, valid_session | ||
<% else %> | ||
put :update, params: {id: <%= file_name %>.to_param, <%= ns_file_name %>: invalid_attributes}, session: valid_session | ||
<% end -%> | ||
JonRowe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
put <%= ns_file_name %>_path(id: <%= file_name %>.to_param, <%= ns_file_name %>: invalid_attributes), headers: valid_headers | ||
|
||
expect(response).to have_http_status(:unprocessable_entity) | ||
expect(response.content_type).to eq('application/json') | ||
end | ||
|
@@ -152,14 +146,9 @@ | |
it "destroys the requested <%= ns_file_name %>" do | ||
<%= file_name %> = <%= class_name %>.create! valid_attributes | ||
expect { | ||
<% if RUBY_VERSION < '1.9.3' -%> | ||
delete :destroy, {:id => <%= file_name %>.to_param}, valid_session | ||
<% else -%> | ||
delete :destroy, params: {id: <%= file_name %>.to_param}, session: valid_session | ||
<% end -%> | ||
delete <%= ns_file_name %>_path(id: <%= file_name %>.to_param), headers: valid_headers | ||
JonRowe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}.to change(<%= class_name %>, :count).by(-1) | ||
end | ||
end | ||
|
||
end | ||
<% end -%> |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,9 +32,7 @@ def generate_controller_spec | |
controller_class_path, | ||
"#{controller_file_name}_controller_spec.rb" | ||
) | ||
if options[:api] | ||
template 'api_controller_spec.rb', template_file | ||
else | ||
unless options[:api] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Basically you need to switch on your request vs controller format here, and still respect existing installtions. |
||
template 'controller_spec.rb', template_file | ||
end | ||
end | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are best left to outside gems, we don't have any other integration with extensions like this, so we can't include this here.