aboutsummaryrefslogtreecommitdiffstats
path: root/guides/source/testing.md
diff options
context:
space:
mode:
authorKir Shatrov <shatrov@me.com>2015-01-03 20:14:05 +0100
committerKir Shatrov <shatrov@me.com>2015-01-03 20:18:56 +0100
commit4a3b9c82830b08056467524081d4dd408eb6db43 (patch)
tree198709d0375fac68bb94bdcd69a01838a627c1ad /guides/source/testing.md
parentbb3c054358901e28a697643b1f76e0a29b68158e (diff)
downloadrails-4a3b9c82830b08056467524081d4dd408eb6db43.tar.gz
rails-4a3b9c82830b08056467524081d4dd408eb6db43.tar.bz2
rails-4a3b9c82830b08056467524081d4dd408eb6db43.zip
Docs for controller test helpers
rails/rails#18305
Diffstat (limited to 'guides/source/testing.md')
-rw-r--r--guides/source/testing.md35
1 files changed, 35 insertions, 0 deletions
diff --git a/guides/source/testing.md b/guides/source/testing.md
index fac3f18eb9..d6e4aa36db 100644
--- a/guides/source/testing.md
+++ b/guides/source/testing.md
@@ -782,6 +782,41 @@ end
Similar to other callbacks in Rails, the `setup` and `teardown` methods can also be used by passing a block, lambda, or method name as a symbol to call.
+### Test helpers
+
+To avoid code duplication, you can add your own test helpers.
+Sign in helper can be a good example:
+
+```ruby
+test/test_helper.rb
+
+module SignInHelper
+ def sign_in(user)
+ session[:user_id] = user.id
+ end
+end
+
+class ActionController::TestCase
+ include SignInHelper
+end
+```
+
+```ruby
+require 'test_helper'
+
+class ProfileControllerTest < ActionController::TestCase
+
+ test "should show profile" do
+ # helper is now reusable from any controller test case
+ sign_in users(:david)
+
+ get :show
+ assert_response :success
+ assert_equal users(:david), assigns(:user)
+ end
+end
+```
+
Testing Routes
--------------