-
Notifications
You must be signed in to change notification settings - Fork 118
Description
Description
Many of the codebase's controllers have corresponding spec/controllers/*_controller_spec.rb test files. Despite PlansController being a core controller in the app, spec/controllers/plans_controller_spec.rb does not exist.
However, rather than writing the controller spec, rspec recommends writing a request spec. The following is from https://rspec.info/blog/2016/07/rspec-3-5-has-been-released/:
In Rails 5 assigns and assert_template are “soft deprecated”. Controller tests themselves are not, and adding :type => :controller to your specs is still 100% supported. Through Rails 3 and 4 it was both prevalent and idiomatic to use assigns in controller specs. As this is a minor release of RSpec our commitment to SemVer means that we are not going to break your existing controller specs. For existing Rails applications that make heavy use of assigns adding the rails-controller-testing to your Gemfile will restore assigns and assert_template. RSpec integrates with this gem seamlessly, so your controller specs should just continue to work.
For new Rails apps: we don’t recommend adding the rails-controller-testing gem to your application. The official recommendation of the Rails team and the RSpec core team is to write request specs instead. Request specs allow you to focus on a single controller action, but unlike controller tests involve the router, the middleware stack, and both rack requests and responses. This adds realism to the test that you are writing, and helps avoid many of the issues that are common in controller specs. In Rails 5, request specs are significantly faster than either request or controller specs were in rails 4, thanks to the work by Eileen Uchitelle[^foot_1] of the Rails Committer Team.
The RSpec Rails 4.0 has been released! blog further indicates a preference for request specs over controller specs:
Improvements to generators
If you use generators, there are some improvements: - The default hash syntax is now the Ruby 1.9 style in generated files to match Rails conventions - Request specs are generated instead of controller specs by default - New generators available (channel, generator, mailbox…)
Proposal
- Add request specs for
PlansController(i.e.spec/requests/plans_controller_spec.rb).- Ensure that this request spec at least covers the main actions of
PlansController
- Ensure that this request spec at least covers the main actions of
Additional Notes
-
Our codebase currently includes the
'rails-controller-testing'gem, which restores the Rails 5–removed methods likeassignsandassert_template.-
These methods were removed because testing controller internals is not recommended. As DHH explains in Deprecate assigns() and assert_template in controller testing rails/rails#18950:
Testing what instance variables are set by your controller is a bad idea. That's grossly overstepping the boundaries of what the test should know about. You can test what cookies are set, what HTTP code is returned, how the view looks, or what mutations happened to the DB, but testing the innards of the controller is just not a good idea.
The same goes for assert_template. This ties the test to the internal structure of how your views are organized. That's not what the test should be testing. It should be testing what DOM elements it expect to be there. Rearranging your templates and partials should not cause these tests to break. Deprecate and recommend using assert_select instead.
-
-
For new code, adding or relying on
rails-controller-testingis discouraged. Instead:- Use request specs to test controller behavior realistically.
- Focus on outcomes (HTTP status, DB changes, responses, page content) rather than internal instance variables or templates.
-
For existing code, it may be worth re-evaluating existing controller specs that rely on
rails-controller-testingand migrating them to request specs. This aligns the codebase with modern Rails and testing best practices and removes reliance on deprecated or discouraged methods.