Skip to content

Generate request specs when scaffold generator is used #2262

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 10 commits into from

Conversation

klyonrad
Copy link
Contributor

@klyonrad klyonrad commented Jan 11, 2020

Closes #2230

Ouf, this is quite some work... 😁

TODO

  • Make the generated specs actually work
  • show and /edit action
  • Implement differences for --api flag
  • Fix the double request spec problem
  • Add appendix with example results into PR body
  • Consistent generation of paths/urls?

Open questions

Integration / smoke test coverage

As far as I understand the smoke tests were testing the controller specs because they were generated by default. So making them not the default reduces the test coverage a bit. Should we take a go at explicitly generating the controller specs in the smoke app too?

Title of the generated request spec

Top title of the generated specs is debatable (See also Comment)

References

Valid_session

The controller specs generated a :valid_session method. I don't know what the equivalent for request specs should be called. The same thing?

valid_headers

Should I take a go at fixing the issue from #2043 for scaffolding?

Naming of the describe blocks

The controller scaffold specs were grouped with PATCH #update etc. The current draft has PATCH /update. This does not fit so well for request specs anymore. Method documentation style #update does not fit because we use actual urls and don't call methods, /update does not fit because that is not really the URL.

We could write PATCH /index_url and GET /resource_name/:id. This fits as long as users stick to the simple rails conventions

Appendix

TODO

Normal

Namespaced

With api flag

With shallow empty file.
Will be filled in later commits with content
@klyonrad klyonrad force-pushed the scaffolds-request-specs branch from 5d0249b to df467bd Compare January 11, 2020 16:34
@klyonrad klyonrad force-pushed the scaffolds-request-specs branch from df467bd to 353967d Compare January 11, 2020 16:41
# sticking to rails and rspec-rails APIs to keep things simple and stable.

<% module_namespacing do -%>
RSpec.describe <%= controller_class_name %>Controller, <%= type_metatag(:request) %> do
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 In an older PR comment You disliked using the word controller in the description title, is that still the case?

Because I would tend to disagree. The specs are describing that specified controller. What else should it be named?

It could also help editor features like "jump to test file", although I didn't see it working in RubyMine (yet)

Copy link
Member

Choose a reason for hiding this comment

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

Request specs don't test controllers, they test specific requests. They route to a certain path and something happens, it could be the controller you intend, it might not. It could be terminated in a rack middleware, it could be routed to a different controller.

@pirj pirj added the v4.0 label Jan 15, 2020
@JonRowe JonRowe added this to the 4.0.0 milestone Jan 15, 2020
@JonRowe
Copy link
Member

JonRowe commented Jan 17, 2020

Ping, how are you getting on with this?

@klyonrad
Copy link
Contributor Author

Ping, how are you getting on with this?

Still have in this in the back on my mind, but didn't have time for it this week

@klyonrad klyonrad force-pushed the scaffolds-request-specs branch from 14bf90f to bd018e3 Compare January 19, 2020 23:54
@klyonrad klyonrad force-pushed the scaffolds-request-specs branch from bd018e3 to 611ff9b Compare January 19, 2020 23:57
@klyonrad
Copy link
Contributor Author

@JonRowe Tests should be green now, the API request spec template is still missing but please have a look already, since I will copy a lot from the current template from the draft now

# Remove skips so we can test controller specs work
gsub_file 'spec/controllers/gadgets_controller_spec.rb',
# Remove skips so we can test that request specs work
gsub_file 'spec/requests/gadgets_request_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.

Could you explain why we have to change that?

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 file that was attempted to edit does not exist anymore. That's what I meant with my question in the PR body about Integration / smoke test coverage

Copy link
Member

Choose a reason for hiding this comment

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

You need to ensure that the smoke coverage covers both scenarios.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

Also added generation with using the --api flag

@@ -7,7 +7,6 @@
run('bin/rake --backtrace spec:requests') || abort
run('bin/rake --backtrace spec:models') || abort
run('bin/rake --backtrace spec:views') || abort
run('bin/rake --backtrace spec:controllers') || abort
Copy link
Member

Choose a reason for hiding this comment

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

Could you explain why we have to change that?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I suspected that the smoke app does not generate controller specs anymore because the default of the ScaffoldGenerator changed

Copy link
Member

Choose a reason for hiding this comment

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

Rather than removing these, you need to add a command to generate some controller tests for this to run.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

Copy link
Contributor Author

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

answering the questions

@@ -7,7 +7,6 @@
run('bin/rake --backtrace spec:requests') || abort
run('bin/rake --backtrace spec:models') || abort
run('bin/rake --backtrace spec:views') || abort
run('bin/rake --backtrace spec:controllers') || abort
Copy link
Member

Choose a reason for hiding this comment

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

Rather than removing these, you need to add a command to generate some controller tests for this to run.

# Remove skips so we can test controller specs work
gsub_file 'spec/controllers/gadgets_controller_spec.rb',
# Remove skips so we can test that request specs work
gsub_file 'spec/requests/gadgets_request_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 need to ensure that the smoke coverage covers both scenarios.

Exclude:
# Splitting the Generator classes would make matters more confusing
# than them being too big
- lib/generators/rspec/**/*
Copy link
Member

Choose a reason for hiding this comment

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

Add the relevant magic comment to the generator that fails rather than doing this, or add the specific files, its possible to extract things without class splitting.

Executing fails though because there is request spec generated
that tries requesting html format
Fixes the problem that ScaffoldsGenerator creates the separate request specs
through the IntegrationGenerator class.

Naming of example resources in generate_stuff task is changed
because I suspect that using the integration generator before scaffolding (with the same name) produced the following error

```
     NameError:
       undefined local variable or method `widgets_index_path' for #<RSpec::ExampleGroups::Widgets::GETWidgets:0x00007f881832e338>
     # ./spec/requests/widgets_spec.rb:6:in `block (3 levels) in <top (required)>'
```

Since this kind of usage does not make sense anyway
I changed the used scaffolding name to product
Copy link
Contributor Author

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

Cannot get the test suite to green 😿

# Remove skips so we can test controller specs work
gsub_file 'spec/controllers/gadgets_controller_spec.rb',
# Remove skips so we can test that request specs work
gsub_file 'spec/requests/gadgets_request_spec.rb',
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

Also added generation with using the --api flag

@@ -7,7 +7,6 @@
run('bin/rake --backtrace spec:requests') || abort
run('bin/rake --backtrace spec:models') || abort
run('bin/rake --backtrace spec:views') || abort
run('bin/rake --backtrace spec:controllers') || abort
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@@ -89,10 +89,12 @@ def using_source_path(path)

generate('model thing name:string')
generate('helper things')
generate('scaffold widget name:string category:string instock:boolean foo_id:integer bar_id:integer --force')
generate('scaffold product name:string category:string instock:boolean foo_id:integer bar_id:integer --force')
Copy link
Contributor Author

Choose a reason for hiding this comment

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

At this point the project stops being fun 😠

Apparently there other tests assumed that the Widget constant is created and I cannot figure how to make the testing of IntegrationGenerator and RequestGenerator work, sometimes the <%= index_helper %>_path works, sometimes or the _url helper.

I still think that generated request specs is important and very useful but for working on this as a lone wolf I am out. So please help, take over oder schedule chatting/pair programming (personally; I am more available in February).


Log of the smoke tests:

Failures:

  1) Using rspec-mocks with models supports stubbing attribute methods on models
     Failure/Error: a_widget = Widget.new
     
     NameError:
       uninitialized constant Widget
     # ./spec/features/model_mocks_integration_spec.rb:10:in `block (2 levels) in <top (required)>'

  2) Using rspec-mocks with models supports stubbing class methods on models
     Failure/Error: allow(Widget).to receive(:all).and_return(:any_stub)
     
     NameError:
       uninitialized constant Widget
     # ./spec/features/model_mocks_integration_spec.rb:5:in `block (2 levels) in <top (required)>'

  3) Widgets GET /widgets works! (now write some real specs)
     Failure/Error: get widgets_index_path
     
     NameError:
       undefined local variable or method `widgets_index_path' for #<RSpec::ExampleGroups::Widgets::GETWidgets:0x00007ff1f27043d8>
     # ./spec/requests/widgets_spec.rb:6:in `block (3 levels) in <top (required)>'

Copy link
Member

Choose a reason for hiding this comment

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

Have you had a chance to take another look? I can lend a hand.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I'll write you an email to chat up or something

@@ -60,8 +78,6 @@ def generate_routing_spec
template 'routing_spec.rb', template_file
end

hook_for :integration_tool, as: :integration
Copy link
Contributor Author

Choose a reason for hiding this comment

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

(Check also my commit message on this change)

I had to dig extremely dig into the git history of the project, tried to figure the purpose and the consequences of this setting (the generator from railties does not have this). But I don't understand it. But is the thing that causes the IntegrationGenerator to run and also generate a request_spec file (figured that out through debugging and lookig at the backtrace).

Changing this seems to cause something, at least that's what the test suite is telling me

See also 98b886b

@JonRowe
Copy link
Member

JonRowe commented Mar 13, 2020

Thanks for your hard work getting this started @klyonrad after playing whack-a-mole on the tests I got this working as #2288, closing this in favour of #2288

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Generate request spec by default when generating scaffolds
4 participants