提交 e258225c 编写于 作者: A Agis Anastasopoulos

Switch to 1.9 hash syntax (guides)

上级 8be3a1a9
...@@ -299,7 +299,7 @@ Now to get this test to pass we can add a model level validation for the _title_ ...@@ -299,7 +299,7 @@ Now to get this test to pass we can add a model level validation for the _title_
```ruby ```ruby
class Post < ActiveRecord::Base class Post < ActiveRecord::Base
validates :title, :presence => true validates :title, presence: true
end end
``` ```
...@@ -397,7 +397,7 @@ Rails adds some custom assertions of its own to the `test/unit` framework: ...@@ -397,7 +397,7 @@ Rails adds some custom assertions of its own to the `test/unit` framework:
| `assert_recognizes(expected_options, path, extras={}, message=nil)` | Asserts that the routing of the given path was handled correctly and that the parsed options (given in the expected_options hash) match path. Basically, it asserts that Rails recognizes the route given by expected_options.| | `assert_recognizes(expected_options, path, extras={}, message=nil)` | Asserts that the routing of the given path was handled correctly and that the parsed options (given in the expected_options hash) match path. Basically, it asserts that Rails recognizes the route given by expected_options.|
| `assert_generates(expected_path, options, defaults={}, extras = {}, message=nil)` | Asserts that the provided options can be used to generate the provided path. This is the inverse of assert_recognizes. The extras parameter is used to tell the request the names and values of additional request parameters that would be in a query string. The message parameter allows you to specify a custom error message for assertion failures.| | `assert_generates(expected_path, options, defaults={}, extras = {}, message=nil)` | Asserts that the provided options can be used to generate the provided path. This is the inverse of assert_recognizes. The extras parameter is used to tell the request the names and values of additional request parameters that would be in a query string. The message parameter allows you to specify a custom error message for assertion failures.|
| `assert_response(type, message = nil)` | Asserts that the response comes with a specific status code. You can specify `:success` to indicate 200-299, `:redirect` to indicate 300-399, `:missing` to indicate 404, or `:error` to match the 500-599 range| | `assert_response(type, message = nil)` | Asserts that the response comes with a specific status code. You can specify `:success` to indicate 200-299, `:redirect` to indicate 300-399, `:missing` to indicate 404, or `:error` to match the 500-599 range|
| `assert_redirected_to(options = {}, message=nil)` | Assert that the redirection options passed in match those of the redirect called in the latest action. This match can be partial, such that `assert_redirected_to(:controller => "weblog")` will also match the redirection of `redirect_to(:controller => "weblog", :action => "show")` and so on.| | `assert_redirected_to(options = {}, message=nil)` | Assert that the redirection options passed in match those of the redirect called in the latest action. This match can be partial, such that `assert_redirected_to(controller: "weblog")` will also match the redirection of `redirect_to(controller: "weblog", action: "show")` and so on.|
| `assert_template(expected = nil, message=nil)` | Asserts that the request was rendered with the appropriate template file.| | `assert_template(expected = nil, message=nil)` | Asserts that the request was rendered with the appropriate template file.|
You'll see the usage of some of these assertions in the next chapter. You'll see the usage of some of these assertions in the next chapter.
...@@ -457,7 +457,7 @@ Let us modify `test_should_create_post` test in `posts_controller_test.rb` so th ...@@ -457,7 +457,7 @@ Let us modify `test_should_create_post` test in `posts_controller_test.rb` so th
```ruby ```ruby
test "should create post" do test "should create post" do
assert_difference('Post.count') do assert_difference('Post.count') do
post :create, :post => { :title => 'Some title'} post :create, post: {title: 'Some title'}
end end
assert_redirected_to post_path(assigns(:post)) assert_redirected_to post_path(assigns(:post))
...@@ -518,7 +518,7 @@ method: ...@@ -518,7 +518,7 @@ method:
test "index should render correct template and layout" do test "index should render correct template and layout" do
get :index get :index
assert_template :index assert_template :index
assert_template :layout => "layouts/application" assert_template layout: "layouts/application"
end end
``` ```
...@@ -528,7 +528,7 @@ things clearer. On the other hand, you have to include the "layouts" directory n ...@@ -528,7 +528,7 @@ things clearer. On the other hand, you have to include the "layouts" directory n
file in this standard layout directory. Hence, file in this standard layout directory. Hence,
```ruby ```ruby
assert_template :layout => "application" assert_template layout: "application"
``` ```
will not work. will not work.
...@@ -541,7 +541,7 @@ Hence: ...@@ -541,7 +541,7 @@ Hence:
```ruby ```ruby
test "new should render correct layout" do test "new should render correct layout" do
get :new get :new
assert_template :layout => "layouts/application", :partial => "_form" assert_template layout: "layouts/application", partial: "_form"
end end
``` ```
...@@ -554,7 +554,7 @@ Here's another example that uses `flash`, `assert_redirected_to`, and `assert_di ...@@ -554,7 +554,7 @@ Here's another example that uses `flash`, `assert_redirected_to`, and `assert_di
```ruby ```ruby
test "should create post" do test "should create post" do
assert_difference('Post.count') do assert_difference('Post.count') do
post :create, :post => { :title => 'Hi', :body => 'This is my first post.'} post :create, post: {title: 'Hi', body: 'This is my first post.'}
end end
assert_redirected_to post_path(assigns(:post)) assert_redirected_to post_path(assigns(:post))
assert_equal 'Post was successfully created.', flash[:notice] assert_equal 'Post was successfully created.', flash[:notice]
...@@ -686,7 +686,7 @@ class UserFlowsTest < ActionDispatch::IntegrationTest ...@@ -686,7 +686,7 @@ class UserFlowsTest < ActionDispatch::IntegrationTest
get "/login" get "/login"
assert_response :success assert_response :success
post_via_redirect "/login", :username => users(:avs).username, :password => users(:avs).password post_via_redirect "/login", username: users(:avs).username, password: users(:avs).password
assert_equal '/welcome', path assert_equal '/welcome', path
assert_equal 'Welcome avs!', flash[:notice] assert_equal 'Welcome avs!', flash[:notice]
...@@ -742,7 +742,7 @@ class UserFlowsTest < ActionDispatch::IntegrationTest ...@@ -742,7 +742,7 @@ class UserFlowsTest < ActionDispatch::IntegrationTest
sess.extend(CustomDsl) sess.extend(CustomDsl)
u = users(user) u = users(user)
sess.https! sess.https!
sess.post "/login", :username => u.username, :password => u.password sess.post "/login", username: u.username, password: u.password
assert_equal '/welcome', path assert_equal '/welcome', path
sess.https!(false) sess.https!(false)
end end
...@@ -803,13 +803,13 @@ class PostsControllerTest < ActionController::TestCase ...@@ -803,13 +803,13 @@ class PostsControllerTest < ActionController::TestCase
end end
test "should show post" do test "should show post" do
get :show, :id => @post.id get :show, id: @post.id
assert_response :success assert_response :success
end end
test "should destroy post" do test "should destroy post" do
assert_difference('Post.count', -1) do assert_difference('Post.count', -1) do
delete :destroy, :id => @post.id delete :destroy, id: @post.id
end end
assert_redirected_to posts_path assert_redirected_to posts_path
...@@ -841,18 +841,18 @@ class PostsControllerTest < ActionController::TestCase ...@@ -841,18 +841,18 @@ class PostsControllerTest < ActionController::TestCase
end end
test "should show post" do test "should show post" do
get :show, :id => @post.id get :show, id: @post.id
assert_response :success assert_response :success
end end
test "should update post" do test "should update post" do
patch :update, :id => @post.id, :post => { } patch :update, id: @post.id, post: {}
assert_redirected_to post_path(assigns(:post)) assert_redirected_to post_path(assigns(:post))
end end
test "should destroy post" do test "should destroy post" do
assert_difference('Post.count', -1) do assert_difference('Post.count', -1) do
delete :destroy, :id => @post.id delete :destroy, id: @post.id
end end
assert_redirected_to posts_path assert_redirected_to posts_path
...@@ -874,7 +874,7 @@ Like everything else in your Rails application, it is recommended that you test ...@@ -874,7 +874,7 @@ Like everything else in your Rails application, it is recommended that you test
```ruby ```ruby
test "should route to post" do test "should route to post" do
assert_routing '/posts/1', { :controller => "posts", :action => "show", :id => "1" } assert_routing '/posts/1', {controller: "posts", action: "show", id: "1"}
end end
``` ```
...@@ -955,7 +955,7 @@ require 'test_helper' ...@@ -955,7 +955,7 @@ require 'test_helper'
class UserControllerTest < ActionController::TestCase class UserControllerTest < ActionController::TestCase
test "invite friend" do test "invite friend" do
assert_difference 'ActionMailer::Base.deliveries.size', +1 do assert_difference 'ActionMailer::Base.deliveries.size', +1 do
post :invite_friend, :email => 'friend@example.com' post :invite_friend, email: 'friend@example.com'
end end
invite_email = ActionMailer::Base.deliveries.last invite_email = ActionMailer::Base.deliveries.last
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册