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

Consistent request spec naming #2356

Closed
wants to merge 3 commits into from

Conversation

eloyesp
Copy link
Contributor

@eloyesp eloyesp commented Jun 17, 2020

Fixes #2355 by re-using the shared examples for "request specs generator" on controllers, that were used scaffold and requests generators.

It also make the generated code by controller generator a little bit better when no actions are added.

Controller generators are using a different naming scheme for requests
specs that are now generated by default. Use always
`spec/requests/posts_spec.rb` by default.

This
Make sure that different generators are consistent on requests reusing
the shared examples.
@@ -15,8 +17,9 @@ class ControllerGenerator < Base
def generate_request_spec
return unless options[:request_specs]

template 'request_spec.rb',
File.join('spec/requests', class_path, "#{file_name}_request_spec.rb")
template_file = actions.empty? ? 'request_spec.rb' : 'request_with_actions_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.

You've renamed the file so where does the alternate come from? Any differences are also untested which I'd like to see rectified!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You've renamed the file so where does the alternate come from?

The alternate template comes from the integration generator (added on the source_paths on line 7).

Any differences are also untested which I'd like to see rectified!

It is reusing functionality of requests generator (and integration generator) because it is just re-using the template, but it is actually being tested on the shared example.

Testing with that shared example we make sure that generators that generate request specs does use a consistent format (they all pass the same specs) while differences between specs are being also being tested.

Copy link
Member

Choose a reason for hiding this comment

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

Maybe generate posts with a scaffold, and generate books with controller generator?

Copy link
Member

Choose a reason for hiding this comment

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

You haven't added a test for the different behaviour, and there is different behaviour now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@JonRowe I did add a shared example that it's testing just that new behavior. You can check that removing that code and specs will fail with meaningful errors.

The specs for that code are in this support file. I'm just adding one line of test because I'm reusing existing specs.

Does it makes sense?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@JonRowe Runing the tests without the implementation changes gives those rspec failures.

@eloyesp
Copy link
Contributor Author

eloyesp commented Jun 18, 2020

It seems that cucumber tests are failing because scaffold generator and controller generator are both trying to generate the same file (that is actually the expected behavior :) ) , but I'm not sure how to solve it.

Update: I've fixed it. 😄

@@ -15,8 +17,9 @@ class ControllerGenerator < Base
def generate_request_spec
return unless options[:request_specs]

template 'request_spec.rb',
File.join('spec/requests', class_path, "#{file_name}_request_spec.rb")
template_file = actions.empty? ? 'request_spec.rb' : 'request_with_actions_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.

You haven't added a test for the different behaviour, and there is different behaviour now.

As now request specs follow the same naming convention the spec
generated by `controller wombats index` does not match the one generated
by `rspec:request wombats` because of the missing action.

Using widgets instead of wombats makes sure both results are identical.
@eloyesp eloyesp requested a review from JonRowe June 18, 2020 16:16
@eloyesp
Copy link
Contributor Author

eloyesp commented Jun 25, 2020

Any update about this?

@eloyesp
Copy link
Contributor Author

eloyesp commented Jun 30, 2020

@JonRowe @pirj Hi, can you review the changes, it seems to me that the change is simple, the only backward incompatible change is that when generating a controller the request spec generated will have a different file name and when it is generated without any action it will add just one spec for an index action with a message about adding a real spec.

All the changes are tested and the controller spec use the shared example for request specs (because it behaves as a request generator).

Copy link
Contributor

@klyonrad klyonrad left a comment

Choose a reason for hiding this comment

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

I am having a look at this now too because I helped with the recent request spec generator changes...

Note also my comment in the issue

@@ -4,9 +4,10 @@

RSpec.describe Rspec::Generators::ControllerGenerator, type: :generator do
setup_default_destination
it_behaves_like "a request spec generator"
Copy link
Contributor

Choose a reason for hiding this comment

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

I am a bit unsure about including this here. What was the goal of it?

Because a couple of tests in that shared example are already in controller_generator_spec so the same things are tested twice.

It appears to me that originally purpose of the example sharing was that integration_test and rspec:request behave exactly the same way

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh I see the other discussion now.... Not to crush your spirit here, but may I suggest to split the two behaviour changes (file naming & content of ControllerGenerator? The ControllerGenerator and the Request/Integration Generator are not the same thing.
There is no inheritance or module connection at all, sometimes repetition in tests is a good thing for understandability

ControllerGenerator executes also the rails generators, while the RequestGenerator only generates the test files (for example to test already existing untested controllers in a codebase)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The ControllerGenerator and the Request/Integration Generator are not the same thing.

My idea here is not make them the same thing, but enforce them to behave as a "request generator". That way, given a certain configuration we can expect them to generate the same request specs.

There is no inheritance or module connection at all, sometimes repetition in tests is a good thing for understandability.

In the case of the controller generator, there is no inheritance, but they share the same template file.

I prefer to use the shared example instead of repeat test, because I like the idea of make sure that this compatibility is maintained in new versions, so the file naming convention becomes a specification for requests generators.

@JonRowe
Copy link
Member

JonRowe commented Aug 24, 2020

Is this replaced by #2376 can this one be closed?

@klyonrad
Copy link
Contributor

Is this replaced by #2376 can this one be closed?

That was my intention, unless @eloyesp vetoes against that :)

@eloyesp
Copy link
Contributor Author

eloyesp commented Aug 26, 2020

There is no need to close it manually, it should close automatically once #2376 is merged (that is the magic of the magic word), I don't see how could help manually closing it, but I'm not against it.

On the other hand, I do like the approach of @klyonrad of splitting the issue on two parts, but there are pending questions against the arguments from @JonRowe about doing both things on the same pull-request, so may be there is still something for me to learn from this.

@JonRowe
Copy link
Member

JonRowe commented Aug 26, 2020

The changes to the generated file names will be merged from #2378 there is still an inconsistency here between the different generated request specs, integration generated request specs have a different content to controller generated request specs, I think the integration/templates/request_spec.rb is a better style than controller/templates/request_spec.rb and should probably be moved to the controller template, and then the integration generator updated to use that template so they have one file as a source of truth.

If you want to add more fleshing out of the actions similar to the scaffold (or prehaps reused) that could be looked at.

If either of you want to tackle that I'd be very happy, as I don't use generators.

@klyonrad
Copy link
Contributor

I can tackle it (after vacation though) since I am way too deep now anyway into these request spec generator issues 😉

@JonRowe
Copy link
Member

JonRowe commented Aug 26, 2020

Thanks, our generators are a bit of a mess, I'd really like someone to do a "project" on cleaning them up

@pirj
Copy link
Member

pirj commented Aug 26, 2020

It's getting better and better with each of your pull request guys, thanks!

@klyonrad
Copy link
Contributor

Ths was replaced by #2378

This was referenced Mar 15, 2021
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.

Generated requests specs are not consistent with naming
4 participants