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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Rails/ControllerTesting cop #638

Merged
merged 1 commit into from
Feb 10, 2022

Conversation

gmcgibbon
Copy link
Contributor

@gmcgibbon gmcgibbon commented Feb 4, 2022

馃憢 I wrote a cop recently to list usage of all old controller tests. Essentially, it replaces ActionController::TestCase with ActionDispatch::IntegrationTest. The actual process of converting between the two test case types is a little more involved, but I think the cop is valuable just to keep track of this tech debt within a codebase. I thought that other developers could possibly benefit from this cop as well. Let me know what you think. Thanks!

Add controller testing cop to discourage use of ActionController::TestCase. ActionDispatch::IntegrationTest should be used instead to test controllers. See https://api.rubyonrails.org/classes/ActionController/TestCase.html

Rails discourages the use of functional tests in favor of integration tests (use ActionDispatch::IntegrationTest).

Related issue for updating the style guide: rubocop/rails-style-guide#302.


Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.
  • The PR relates to only one subject with a clear title
    and description in grammatically correct, complete sentences.
  • If this is a new cop, consider making a corresponding update to the Rails Style Guide.

@gmcgibbon gmcgibbon force-pushed the controller_testing_cop branch 3 times, most recently from b7046b9 to 8405f86 Compare February 4, 2022 23:29
Copy link
Member

@pirj pirj left a comment

Choose a reason for hiding this comment

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

Thank you!

config/default.yml Outdated Show resolved Hide resolved
config/default.yml Outdated Show resolved Hide resolved
spec/rubocop/cop/rails/controller_testing_spec.rb Outdated Show resolved Hide resolved
config/default.yml Outdated Show resolved Hide resolved
expect_no_offenses(<<~RUBY)
class MyControllerTest < ActionDispatch::IntegrationTest; end
RUBY
end
Copy link
Member

Choose a reason for hiding this comment

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

Can you add a test case that does not have a superclass as a test?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added one that tests:

    expect_no_offenses(<<~RUBY)
      class MyControllerTest < SuperControllerTest
      end
    RUBY

for a custom superclass. Let me know if you meant that, or something else 馃槃

Copy link
Member

Choose a reason for hiding this comment

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

I was worried about class Foo; end, but there was no problem :-)

config/default.yml Outdated Show resolved Hide resolved
config/default.yml Outdated Show resolved Hide resolved
@gmcgibbon gmcgibbon force-pushed the controller_testing_cop branch 2 times, most recently from f2ac26a to b33f717 Compare February 7, 2022 17:06
config/default.yml Outdated Show resolved Hide resolved
lib/rubocop/cop/rails/action_controller_testing.rb Outdated Show resolved Hide resolved
lib/rubocop/cop/rails/action_controller_testing.rb Outdated Show resolved Hide resolved
lib/rubocop/cop/rails/action_controller_testing.rb Outdated Show resolved Hide resolved
lib/rubocop/cop/rails/action_controller_testing.rb Outdated Show resolved Hide resolved
lib/rubocop/cop/rails/action_controller_testing.rb Outdated Show resolved Hide resolved
config/default.yml Outdated Show resolved Hide resolved
Add controller testing cop to discourage use of
ActionController::TestCase. ActionDispatch::IntegrationTest should be
used instead to test controllers.
@koic koic merged commit 74be4d6 into rubocop:master Feb 10, 2022
@koic
Copy link
Member

koic commented Feb 10, 2022

Thanks!

@gmcgibbon gmcgibbon deleted the controller_testing_cop branch February 10, 2022 17:48
@gmcgibbon
Copy link
Contributor Author

Thank you both for the reviews, I learned a lot! 鉂わ笍

koic added a commit to koic/rubocop that referenced this pull request Feb 12, 2022
Follow up of rubocop/rubocop-rails#638 (comment).

This PR adds new `InternalAffairs/RedundantContextConfigParameter` cop
that checks for redundant `:config` parameter in the `context` arguments.

```ruby
# bad
context 'foo', :config do
end

# good
context 'foo' do
end
```
bbatsov pushed a commit to rubocop/rubocop that referenced this pull request Feb 13, 2022
Follow up of rubocop/rubocop-rails#638 (comment).

This PR adds new `InternalAffairs/RedundantContextConfigParameter` cop
that checks for redundant `:config` parameter in the `context` arguments.

```ruby
# bad
context 'foo', :config do
end

# good
context 'foo' do
end
```
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.

None yet

3 participants