diff options
author | Kir Shatrov <shatrov@me.com> | 2015-01-04 10:35:06 +0100 |
---|---|---|
committer | Kir Shatrov <shatrov@me.com> | 2015-01-29 14:44:46 +0200 |
commit | baf14ae513337cb185acf865e93dfc48f3aabf6a (patch) | |
tree | 77235d82964272c8721a62ae046b16279d01f945 /guides/source/testing.md | |
parent | 069b72aaf04d2caef76f8e71f320716129f2d949 (diff) | |
download | rails-baf14ae513337cb185acf865e93dfc48f3aabf6a.tar.gz rails-baf14ae513337cb185acf865e93dfc48f3aabf6a.tar.bz2 rails-baf14ae513337cb185acf865e93dfc48f3aabf6a.zip |
Switch to kwargs in ActionController::TestCase and ActionDispatch::Integration
Non-kwargs requests are deprecated now.
Guides are updated as well.
`post url, nil, nil, { a: 'b' }` doesn't make sense.
`post url, params: { y: x }, session: { a: 'b' }` would be an explicit way to do the same
Diffstat (limited to 'guides/source/testing.md')
-rw-r--r-- | guides/source/testing.md | 45 |
1 files changed, 27 insertions, 18 deletions
diff --git a/guides/source/testing.md b/guides/source/testing.md index fa55c09c64..50892dd5e2 100644 --- a/guides/source/testing.md +++ b/guides/source/testing.md @@ -480,21 +480,28 @@ In the `test_should_get_index` test, Rails simulates a request on the action cal The `get` method kicks off the web request and populates the results into the response. It accepts 4 arguments: -* The action of the controller you are requesting. This can be in the form of a string or a symbol. -* An optional hash of request parameters to pass into the action (eg. query string parameters or article variables). -* An optional hash of session variables to pass along with the request. -* An optional hash of flash values. +* The action of the controller you are requesting. + This can be in the form of a string or a symbol. + +* `params:` option with a hash of request parameters to pass into the action + (e.g. query string parameters or article variables). + +* `session:` option with a hash of session variables to pass along with the request. + +* `flash:` option with a hash of flash values. + +All the keyword arguments are optional. Example: Calling the `:show` action, passing an `id` of 12 as the `params` and setting a `user_id` of 5 in the session: ```ruby -get(:show, {'id' => "12"}, {'user_id' => 5}) +get(:show, params: {'id' => "12"}, session: {'user_id' => 5}) ``` Another example: Calling the `:view` action, passing an `id` of 12 as the `params`, this time with no session, but with a flash message. ```ruby -get(:view, {'id' => '12'}, nil, {'message' => 'booya!'}) +get(:view, params: {'id' => '12'}, flash: {'message' => 'booya!'}) ``` 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. @@ -504,7 +511,7 @@ Let us modify `test_should_create_article` test in `articles_controller_test.rb` ```ruby test "should create article" do assert_difference('Article.count') do - post :create, article: {title: 'Some title'} + post :create, params: { article: {title: 'Some title'} } end assert_redirected_to article_path(assigns(:article)) @@ -534,7 +541,7 @@ NOTE: Functional tests do not verify whether the specified request type is accep ```ruby test "ajax request responds with no layout" do - xhr :get, :show, id: articles(:first).id + xhr :get, :show, params: { id: articles(:first).id } assert_template :index assert_template layout: nil @@ -638,7 +645,7 @@ Let's start by adding this assertion to our `test_should_create_article` test: ```ruby test "should create article" do assert_difference('Article.count') do - post :create, article: {title: 'Some title'} + post :create, params: { article: {title: 'Some title'} } end assert_redirected_to article_path(assigns(:article)) @@ -708,7 +715,7 @@ Let's write a test for the `:show` action: ```ruby test "should show article" do article = articles(:one) - get :show, id: article.id + get :show, params: { id: article.id } assert_response :success end ``` @@ -721,7 +728,7 @@ How about deleting an existing Article? test "should destroy article" do article = articles(:one) assert_difference('Article.count', -1) do - delete :destroy, id: article.id + delete :destroy, params: { id: article.id } end assert_redirected_to articles_path @@ -733,7 +740,7 @@ We can also add a test for updating an existing Article. ```ruby test "should update article" do article = articles(:one) - patch :update, id: article.id, article: {title: "updated"} + patch :update, params: { id: article.id, article: {title: "updated"} } assert_redirected_to article_path(assigns(:article)) end ``` @@ -759,20 +766,20 @@ class ArticlesControllerTest < ActionController::TestCase test "should show article" do # Reuse the @article instance variable from setup - get :show, id: @article.id + get :show, params: { id: @article.id } assert_response :success end test "should destroy article" do assert_difference('Article.count', -1) do - delete :destroy, id: @article.id + delete :destroy, params: { id: @article.id } end assert_redirected_to articles_path end test "should update article" do - patch :update, id: @article.id, article: {title: "updated"} + patch :update, params: { id: @article.id, article: {title: "updated"} } assert_redirected_to article_path(assigns(:article)) end end @@ -1026,7 +1033,8 @@ test "can create an article" do assert_response :success assert_template "articles/new", partial: "articles/_form" - post "/articles", article: {title: "can create", body: "article successfully."} + post "/articles", + params: { article: {title: "can create", body: "article successfully."} } assert_response :redirect follow_redirect! assert_response :success @@ -1042,7 +1050,8 @@ We start by calling the `:new` action on our Articles controller. This response After this we make a post request to the `:create` action of our Articles controller: ```ruby -post "/articles", article: {title: "can create", body: "article successfully."} +post "/articles", + params: { article: {title: "can create", body: "article successfully."} } assert_response :redirect follow_redirect! ``` @@ -1147,7 +1156,7 @@ require 'test_helper' class UserControllerTest < ActionController::TestCase test "invite friend" do assert_difference 'ActionMailer::Base.deliveries.size', +1 do - post :invite_friend, email: 'friend@example.com' + post :invite_friend, params: { email: 'friend@example.com' } end invite_email = ActionMailer::Base.deliveries.last |