Skip to content

Commit 4414c5d

Browse files
committed
Remove ActionController::TestCase from documentation
In Rails 5.1 `ActionController::TestCase` will be moved out of Rails into it's own gem. Please use `ActionDispatch::IntegrationTest` going foward. Because this will be moved to a gem I used `# :stopdoc:` instead of deleting the documentation. This will remove it from the Rails documentation but still leave the method documented for when we move it to a gem. Guides have been updated to use the routing structure used in Integration and all test examples have been updated to inherit from `ActionDispatch::IntegrationTest` instead of `ActionController::TestCase. Fixes #22496
1 parent eb0e8e2 commit 4414c5d

File tree

6 files changed

+51
-35
lines changed

6 files changed

+51
-35
lines changed

actionpack/CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
* `ActionController::TestCase` will be moved to it's own gem in Rails 5.1
2+
3+
With the speed improvements made to `ActionDispatch::IntegrationTest` we no
4+
longer need to keep two separate code bases for testing controllers. In
5+
Rails 5.1 `ActionController::TestCase` will be deprecated and moved into a
6+
gem outside of Rails source.
7+
8+
This is a documentation deprecation so that going forward so new tests will use
9+
`ActionDispatch::IntegrationTest` instead of `ActionController::TestCase`.
10+
11+
*Eileen M. Uchitelle*
12+
113
* Add a `response_format` option to `ActionDispatch::DebugExceptions`
214
to configure the format of the response when errors occur in
315
development mode.

actionpack/lib/action_controller/test_case.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
require 'rails-dom-testing'
88

99
module ActionController
10+
# :stopdoc:
11+
# ActionController::TestCase will be deprecated and moved to a gem in Rails 5.1.
12+
# Please use ActionDispatch::IntegrationTest going forward.
1013
class TestRequest < ActionDispatch::TestRequest #:nodoc:
1114
DEFAULT_ENV = ActionDispatch::TestRequest::DEFAULT_ENV.dup
1215
DEFAULT_ENV.delete 'PATH_INFO'
@@ -658,4 +661,5 @@ def html_format?(parameters)
658661

659662
include Behavior
660663
end
664+
# :startdoc:
661665
end

actionpack/lib/action_dispatch/testing/test_process.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def redirect_to_url
2626
@response.redirect_url
2727
end
2828

29-
# Shortcut for <tt>Rack::Test::UploadedFile.new(File.join(ActionController::TestCase.fixture_path, path), type)</tt>:
29+
# Shortcut for <tt>Rack::Test::UploadedFile.new(File.join(ActionDispatch::IntegrationTest.fixture_path, path), type)</tt>:
3030
#
3131
# post :change_avatar, avatar: fixture_file_upload('files/spongebob.png', 'image/png')
3232
#

guides/source/active_support_core_extensions.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -517,17 +517,17 @@ Extensions to `Module`
517517

518518
Using plain Ruby you can wrap methods with other methods, that's called _alias chaining_.
519519

520-
For example, let's say you'd like params to be strings in functional tests, as they are in real requests, but still want the convenience of assigning integers and other kind of values. To accomplish that you could wrap `ActionController::TestCase#process` this way in `test/test_helper.rb`:
520+
For example, let's say you'd like params to be strings in functional tests, as they are in real requests, but still want the convenience of assigning integers and other kind of values. To accomplish that you could wrap `ActionDispatch::IntegrationTest#process` this way in `test/test_helper.rb`:
521521

522522
```ruby
523-
ActionController::TestCase.class_eval do
523+
ActionDispatch::IntegrationTest.class_eval do
524524
# save a reference to the original process method
525525
alias_method :original_process, :process
526526

527527
# now redefine process and delegate to original_process
528-
def process(action, params=nil, session=nil, flash=nil, http_method='GET')
528+
def process('GET', path, params: nil, headers: nil, env: nil, xhr: false)
529529
params = Hash[*params.map {|k, v| [k, v.to_s]}.flatten]
530-
original_process(action, params, session, flash, http_method)
530+
original_process('GET', path, params: params)
531531
end
532532
end
533533
```
@@ -537,10 +537,10 @@ That's the method `get`, `post`, etc., delegate the work to.
537537
That technique has a risk, it could be the case that `:original_process` was taken. To try to avoid collisions people choose some label that characterizes what the chaining is about:
538538

539539
```ruby
540-
ActionController::TestCase.class_eval do
540+
ActionDispatch::IntegrationTest.class_eval do
541541
def process_with_stringified_params(...)
542542
params = Hash[*params.map {|k, v| [k, v.to_s]}.flatten]
543-
process_without_stringified_params(action, params, session, flash, http_method)
543+
process_without_stringified_params(method, path, params: params)
544544
end
545545
alias_method :process_without_stringified_params, :process
546546
alias_method :process, :process_with_stringified_params
@@ -550,10 +550,10 @@ end
550550
The method `alias_method_chain` provides a shortcut for that pattern:
551551

552552
```ruby
553-
ActionController::TestCase.class_eval do
553+
ActionDispatch::IntegrationTest.class_eval do
554554
def process_with_stringified_params(...)
555555
params = Hash[*params.map {|k, v| [k, v.to_s]}.flatten]
556-
process_without_stringified_params(action, params, session, flash, http_method)
556+
process_without_stringified_params(method, path, params: params)
557557
end
558558
alias_method_chain :process, :stringified_params
559559
end

guides/source/engines.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,9 +1033,9 @@ typical `GET` to a controller in a controller's functional test like this:
10331033

10341034
```ruby
10351035
module Blorgh
1036-
class FooControllerTest < ActionController::TestCase
1036+
class FooControllerTest < ActionDispatch::IntegrationTest
10371037
def test_index
1038-
get :index
1038+
get foos_url
10391039
...
10401040
end
10411041
end
@@ -1049,13 +1049,13 @@ in your setup code:
10491049

10501050
```ruby
10511051
module Blorgh
1052-
class FooControllerTest < ActionController::TestCase
1052+
class FooControllerTest < ActionDispatch::IntegrationTest
10531053
setup do
10541054
@routes = Engine.routes
10551055
end
10561056

10571057
def test_index
1058-
get :index
1058+
get foos_url
10591059
...
10601060
end
10611061
end

guides/source/testing.md

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,6 @@ You'll see the usage of some of these assertions in the next chapter.
330330
All the basic assertions such as `assert_equal` defined in `Minitest::Assertions` are also available in the classes we use in our own test cases. In fact, Rails provides the following classes for you to inherit from:
331331
332332
* `ActiveSupport::TestCase`
333-
* `ActionController::TestCase`
334333
* `ActionMailer::TestCase`
335334
* `ActionView::TestCase`
336335
* `ActionDispatch::IntegrationTest`
@@ -682,9 +681,9 @@ Let me take you through one such test, `test_should_get_index` from the file `ar
682681
683682
```ruby
684683
# articles_controller_test.rb
685-
class ArticlesControllerTest < ActionController::TestCase
684+
class ArticlesControllerTest < ActionDispatch::IntegrationTest
686685
test "should get index" do
687-
get :index
686+
get '/articles'
688687
assert_response :success
689688
assert_includes @response.body, 'Articles'
690689
end
@@ -697,7 +696,7 @@ and also ensuring that the right response body has been generated.
697696
The `get` method kicks off the web request and populates the results into the response. It accepts 4 arguments:
698697
699698
* The action of the controller you are requesting.
700-
This can be in the form of a string or a symbol.
699+
This can be in the form of a string or a route (i.e. `articles_url`).
701700
702701
* `params`: option with a hash of request parameters to pass into the action
703702
(e.g. query string parameters or article variables).
@@ -717,7 +716,7 @@ get(:show, params: { id: 12 }, session: { user_id: 5 })
717716
Another example: Calling the `:view` action, passing an `id` of 12 as the `params`, this time with no session, but with a flash message.
718717
719718
```ruby
720-
get(:view, params: { id: 12 }, flash: { message: 'booya!' })
719+
get(view_url, params: { id: 12 }, flash: { message: 'booya!' })
721720
```
722721
723722
NOTE: If you try running `test_should_create_article` test from `articles_controller_test.rb` it will fail on account of the newly added model level validation and rightly so.
@@ -727,7 +726,7 @@ Let us modify `test_should_create_article` test in `articles_controller_test.rb`
727726
```ruby
728727
test "should create article" do
729728
assert_difference('Article.count') do
730-
post :create, params: { article: { title: 'Some title' } }
729+
post '/article', params: { article: { title: 'Some title' } }
731730
end
732731
733732
assert_redirected_to article_path(Article.last)
@@ -758,7 +757,8 @@ To test AJAX requests, you can specify the `xhr: true` option to `get`, `post`,
758757
759758
```ruby
760759
test "ajax request" do
761-
get :show, params: { id: articles(:first).id }, xhr: true
760+
article = articules(:first)
761+
get article_url(article), xhr: true
762762
763763
assert_equal 'hello world', @response.body
764764
assert_equal "text/javascript", @response.content_type
@@ -799,11 +799,11 @@ can be set directly on the `@request` instance variable:
799799
```ruby
800800
# setting a HTTP Header
801801
@request.headers["Accept"] = "text/plain, text/html"
802-
get :index # simulate the request with custom header
802+
get articles_url # simulate the request with custom header
803803
804804
# setting a CGI variable
805805
@request.headers["HTTP_REFERER"] = "http://example.com/home"
806-
post :create # simulate the request with custom env variable
806+
post article_url # simulate the request with custom env variable
807807
```
808808
809809
### Testing `flash` notices
@@ -818,7 +818,7 @@ Let's start by adding this assertion to our `test_should_create_article` test:
818818
```ruby
819819
test "should create article" do
820820
assert_difference('Article.count') do
821-
post :create, params: { article: { title: 'Some title' } }
821+
post article_url, params: { article: { title: 'Some title' } }
822822
end
823823
824824
assert_redirected_to article_path(Article.last)
@@ -888,7 +888,7 @@ Let's write a test for the `:show` action:
888888
```ruby
889889
test "should show article" do
890890
article = articles(:one)
891-
get :show, params: { id: article.id }
891+
get '/article', params: { id: article.id }
892892
assert_response :success
893893
end
894894
```
@@ -901,7 +901,7 @@ How about deleting an existing Article?
901901
test "should destroy article" do
902902
article = articles(:one)
903903
assert_difference('Article.count', -1) do
904-
delete :destroy, params: { id: article.id }
904+
delete article_url(article)
905905
end
906906
907907
assert_redirected_to articles_path
@@ -913,7 +913,7 @@ We can also add a test for updating an existing Article.
913913
```ruby
914914
test "should update article" do
915915
article = articles(:one)
916-
patch :update, params: { id: article.id, article: { title: "updated" } }
916+
patch '/article', params: { id: article.id, article: { title: "updated" } }
917917
assert_redirected_to article_path(article)
918918
end
919919
```
@@ -925,7 +925,7 @@ Our test should now look something like this, disregard the other tests we're le
925925
```ruby
926926
require 'test_helper'
927927
928-
class ArticlesControllerTest < ActionController::TestCase
928+
class ArticlesControllerTest < ActionDispatch::IntegrationTest
929929
# called before every single test
930930
setup do
931931
@article = articles(:one)
@@ -939,20 +939,20 @@ class ArticlesControllerTest < ActionController::TestCase
939939
940940
test "should show article" do
941941
# Reuse the @article instance variable from setup
942-
get :show, params: { id: @article.id }
942+
get article_url(@article)
943943
assert_response :success
944944
end
945945
946946
test "should destroy article" do
947947
assert_difference('Article.count', -1) do
948-
delete :destroy, params: { id: @article.id }
948+
delete article_url(@article)
949949
end
950950
951951
assert_redirected_to articles_path
952952
end
953953
954954
test "should update article" do
955-
patch :update, params: { id: @article.id, article: { title: "updated" } }
955+
patch article_url(@article), params: { article: { title: "updated" } }
956956
assert_redirected_to article_path(@article)
957957
end
958958
end
@@ -974,21 +974,21 @@ module SignInHelper
974974
end
975975
end
976976
977-
class ActionController::TestCase
977+
class ActionDispatch::IntegrationTest
978978
include SignInHelper
979979
end
980980
```
981981
982982
```ruby
983983
require 'test_helper'
984984
985-
class ProfileControllerTest < ActionController::TestCase
985+
class ProfileControllerTest < ActionDispatch::IntegrationTest
986986
987987
test "should show profile" do
988988
# helper is now reusable from any controller test case
989989
sign_in users(:david)
990990
991-
get :show
991+
get profile_url
992992
assert_response :success
993993
end
994994
end
@@ -1186,10 +1186,10 @@ Functional testing for mailers involves more than just checking that the email b
11861186
```ruby
11871187
require 'test_helper'
11881188
1189-
class UserControllerTest < ActionController::TestCase
1189+
class UserControllerTest < ActionDispatch::IntegrationTest
11901190
test "invite friend" do
11911191
assert_difference 'ActionMailer::Base.deliveries.size', +1 do
1192-
post :invite_friend, params: { email: 'friend@example.com' }
1192+
post invite_friend_url, params: { email: 'friend@example.com' }
11931193
end
11941194
invite_email = ActionMailer::Base.deliveries.last
11951195

0 commit comments

Comments
 (0)