Skip to content

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

Closed
wants to merge 9 commits into from

Conversation

exocode
Copy link

@exocode exocode commented Dec 7, 2018

Basic json header is added and could be customized later by the user (when using Authentication logic).
Usage:
generate scaffold v1::test name:string --api
Automagically knows if factory_bot or fabrication gem is installed and places their corresponding generators within the valid_attributes hash.
This behaviour can also be overrided by using --fabrication or --factory

In order to work properly, some methods were copied from the controller_spec.

Related: #2043

Basic json header is added and could be customized later by the user. For example when using Authentication logic.
`generate scaffold v1::test name:string --api` automagically knows if `factory_bot` or `fabrication` gem is installed and places their corresponding generators within the `valid_attributes` hash.
This behaviour can also be overrided by using `--fabrication` or `--factory`
In order to work properly, some methods were copied from the controller_spec.
Copy link
Member

@JonRowe JonRowe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good start! Some early feedback for you

@@ -33,7 +33,8 @@ def generate_controller_spec
"#{controller_file_name}_controller_spec.rb"
)
if options[:api]
template 'api_controller_spec.rb', template_file
# skip generation of controller_spec. Specs are in request_spec if an API is scaffolded
# template 'api_controller_spec.rb', template_file
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't commit commented out code, you will need a:

# For api templates we generate a request spec instead
unless options[:api]
  template 'controller_spec.rb', template_file
end

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And remove the old file too.

@@ -10,12 +10,64 @@ 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 :factory, :type => :boolean, :desc => "Fill params with Factory Bot aka Factory Girl model attributes"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this option is specific to factory bot then it should be called factorybot for consistency, also can you remove the aka Factory Girl reference please, most people know its changed and I'd rather we didn't drag up the debate.

template_file = File.join(
'spec/requests',
class_path,
"#{table_name}_requests_spec.rb"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need requests twice.

# 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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Multi line lets should be do/end.

# <%= class_name %>. As you add validations to <%= class_name %>, be sure to
# adjust the attributes here as well.
let(:valid_attributes) {
<% if options[:factory] || Gem.loaded_specs.has_key?('factory_bot_rails') -%>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rather we make these configurations opt in only, rather than magically turn them on, what if Rubygems is not available (quite possible, we turn it off in our builds), also what if they have a different gem installed? (Just factory bot, or the older name for the gem :P)



<% module_namespacing do -%>
RSpec.describe <%= class_name.pluralize %>Controller, <%= type_metatag(:request) %> do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not controller...

it "returns a success response" do
<%= file_name %> = <%= class_name %>.create! valid_attributes

get <%= ns_file_name.pluralize %>_path, params: {}, headers: valid_headers
Copy link
Member

Choose a reason for hiding this comment

The 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 :/

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont understand what you mean... :-/

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Older versions of rails accepted a hash of options to their get/post/put etc methods, some time around Rails 4 this was changed to keyword arguments. If these are keyword arguments this is fine, but if this is a hash of arguments we tend to favour the hashrocket :key => value style due to older Ruby support. Did you copy these from the controller equivalent? If so thats fine as this will be the same level of support.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs resolving

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can now safely be ignored, you can use rails 5 syntax.

@exocode
Copy link
Author

exocode commented Oct 14, 2019

Can someone please overfly and tell me what exactly is missing or I have to change?
Rails API is a core feature that I use. I always have to delete the controller_spec where a request_spec is needed.

Copy link
Member

@JonRowe JonRowe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @exocode, the basic premises of the review is that I'm happy to add this is it meets the following criteria:

  • All versions of Rails / Ruby that we support must be supported. That is the main crux of the issue right now.

  • That this is a configurable option for generation, so existing people wanting to generate controller specs still can, so you need to duplicate the templates rather than modify existing ones. In rspec-rails 4 this can be made the default.

  • You need to remove the support for 3rd party tools, that doesn't belong here.

@@ -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"
Copy link
Member

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.

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"


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Duplicate spacing

template 'request_spec.rb',
File.join('spec/requests', class_path, "#{table_name}_spec.rb")
end

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excess whitespace and weird line formatting.


attr_reader :generator_args

# @todo refactor the following methods. They are also in the /lib/generators/rpsec/scaffold/scaffold_generator.rb file.
Copy link
Member

Choose a reason for hiding this comment

The 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.

# 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.

Copy link
Member

Choose a reason for hiding this comment

The 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.

@pirj
Copy link
Member

pirj commented Jan 8, 2020

@exocode are you interested in finishing this? This contribution would be very valuable.

@exocode
Copy link
Author

exocode commented Jan 9, 2020

@exocode are you interested in finishing this? This contribution would be very valuable.

I will continue that! At the moment I've to finish another project urgently. I will give it a try

@pirj
Copy link
Member

pirj commented Mar 13, 2020

@exocode Do you think this still applies after https://github.com/rspec/rspec-rails/pull/2288/files#diff-251cf12c9198d8742474434672773adeR1 being merged?

@JonRowe
Copy link
Member

JonRowe commented Mar 13, 2020

I'm going to close this for now as there is an api option to generate request specs when scaffolded and for controllers. Please try the new generators and open a new PR if you feel they can be improved!

@JonRowe JonRowe closed this Mar 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants