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

Favor request specs over controller specs when generating a controller #2222

Merged
merged 4 commits into from Dec 20, 2019

Conversation

klyonrad
Copy link
Contributor

@klyonrad klyonrad commented Dec 7, 2019

Closes #2056

Open questions in my mind (and why I consider this a work in progress):

  1. Should I take care of the scaffolding generators as well?
  2. Should the request spec templates have more content?

EDIT:

Regarding 2: Yes, they should
Regarding 1: I created a separate issue for this. See #2230

@@ -0,0 +1,10 @@
require 'rails_helper'

RSpec.describe "<%= class_name.pluralize %>", <%= 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.

Copied over from lib/generators/rspec/integration/templates/request_spec.rb

I didn't try "very hard" to use the same template file because I am not sure if that is even wished for

Copy link
Member

Choose a reason for hiding this comment

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

I'd recommend writing a spec for get name etc that returns success or something similarly basic.

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, figuring the details for the templates out was/is a bit difficult for me. I am sure, I would figure it out - but can you clarify the specs a bit maybe? I mean, what does the rspec-rails team think should be the convention for request specs that test a controller

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

I mean, what does the rspec-rails team think should be the convention for request specs that test a controller

I don't use generators myself as I find that the similar lines tend to just be the naming only. There isn't that much of a semantic difference between a controller spec and a request spec, its the level of stack integration that differs. Personally I would be happy with:

describe "GET /<%= name.underscore.pluralize %>" do
  it "works! (now write some real specs)" do
    get "/<%= name.underscore.pluralize %>"
    expect(response).to be_success
  end
end

However finesse wise I think that name.underscore.pluralize is wrong here, because it needs to be name spaced (or not) appropriately.

Copy link
Member

Choose a reason for hiding this comment

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

Let me know if you want any more feedback

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 went with doing the same thing like the existing controller spec templates


If I understand the rails controller generator code correctly
it does note generate any routes unless we gave actions as a parameter. So the lib/generators/rspec/controller/templates/controller_spec.rb template makes a very empty controller spec

require 'rails_helper'

RSpec.describe Admin::CustomersController, type: :controller do

end

So when the generator is generated without action names it should not have a test. Otherwise that test would not be green

@klyonrad klyonrad force-pushed the controller-default-generated-specs branch from 31b3054 to e322e49 Compare December 7, 2019 14:36
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.

Not sure if you wanted feedback on a draft but this is a good start!

lib/generators/rspec/controller/controller_generator.rb Outdated Show resolved Hide resolved
@@ -0,0 +1,10 @@
require 'rails_helper'

RSpec.describe "<%= class_name.pluralize %>", <%= 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.

I'd recommend writing a spec for get name etc that returns success or something similarly basic.

@pirj
Copy link
Member

pirj commented Dec 8, 2019

Up for discussion, should we omit generation of controller specs altogether? Brought up here.

generators.test_framework :rspec, controller_specs: false

Related code.

@JonRowe
Copy link
Member

JonRowe commented Dec 8, 2019

Up for discussion, should we omit generation of controller specs altogether? Brought up here.

No changing the default is sufficient

@klyonrad klyonrad force-pushed the controller-default-generated-specs branch from 5c0ce51 to 764de75 Compare December 15, 2019 20:04
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.

  • branch is rebased
  • rails version checks are removed
  • I tried to make the template similar to the controller spec templates

travis is also failing on the master branch. Do not understand why. I tried to use the method that the deprecation warning in the sanity check recommends (See klyonrad@cfcbd27) but this then breaks in another way

Therefore consider this not a draft anymore, when the sanity check thing is fixed, I will rebase and maybe squash the commits

@@ -0,0 +1,10 @@
require 'rails_helper'

RSpec.describe "<%= class_name.pluralize %>", <%= 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.

I went with doing the same thing like the existing controller spec templates


If I understand the rails controller generator code correctly
it does note generate any routes unless we gave actions as a parameter. So the lib/generators/rspec/controller/templates/controller_spec.rb template makes a very empty controller spec

require 'rails_helper'

RSpec.describe Admin::CustomersController, type: :controller do

end

So when the generator is generated without action names it should not have a test. Otherwise that test would not be green

@klyonrad klyonrad marked this pull request as ready for review December 15, 2019 22:23
@benoittgt
Copy link
Member

For the issue on master branch. I will try to fix it with. #2231

@klyonrad klyonrad force-pushed the controller-default-generated-specs branch from 349719a to 5614581 Compare December 20, 2019 14:18
@JonRowe JonRowe merged commit db2aeec into rspec:master Dec 20, 2019
@JonRowe
Copy link
Member

JonRowe commented Dec 20, 2019

Thanks for your patience here!

JonRowe added a commit that referenced this pull request Dec 20, 2019
@klyonrad klyonrad deleted the controller-default-generated-specs branch December 20, 2019 15:19
JonRowe added a commit that referenced this pull request Jan 12, 2020
…pecs

Favor request specs over controller specs when generating a controller
JonRowe added a commit that referenced this pull request Jan 12, 2020
JonRowe added a commit that referenced this pull request Mar 10, 2020
…erated-specs"

This reverts commit db2aeec, reversing
changes made to 7dc567f.
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.

Generate request spec by default when generating controller
4 participants