diff options
author | Gannon McGibbon <gannon.mcgibbon@gmail.com> | 2018-12-04 18:48:35 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-12-04 18:48:35 -0500 |
commit | d4ad9b2fc15550328bcc7cfd382cd3094a36faa3 (patch) | |
tree | 5b68b445ba0fff9aed3bbaafde583b0a987ded7e /guides/source | |
parent | 1decfedb2b3d7047d9696ee054ac8778d396372c (diff) | |
parent | d45b74e89708bb26c68f04e17090eb1c56898ed8 (diff) | |
download | rails-d4ad9b2fc15550328bcc7cfd382cd3094a36faa3.tar.gz rails-d4ad9b2fc15550328bcc7cfd382cd3094a36faa3.tar.bz2 rails-d4ad9b2fc15550328bcc7cfd382cd3094a36faa3.zip |
Merge pull request #34563 from sambostock/improve-test-helper-guides
Add advanced test helpers docs to guides
Diffstat (limited to 'guides/source')
-rw-r--r-- | guides/source/testing.md | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/guides/source/testing.md b/guides/source/testing.md index 02b60a395b..9541598b26 100644 --- a/guides/source/testing.md +++ b/guides/source/testing.md @@ -1397,6 +1397,56 @@ class ProfileControllerTest < ActionDispatch::IntegrationTest end ``` +#### Using Separate Files + +If you find your helpers are cluttering `test_helper.rb`, you can extract them into separate files. One good place to store them is `lib/test`. + +```ruby +# lib/test/multiple_assertions.rb +module MultipleAssertions + def assert_multiple_of_fourty_two(number) + assert (number % 42 == 0), 'expected #{number} to be a multiple of 42' + end +end +``` + +These helpers can then be explicitly required as needed and included as needed + +```ruby +require 'test_helper' +require 'test/multiple_assertions' + +class NumberTest < ActiveSupport::TestCase + include MultipleAssertions + + test '420 is a multiple of fourty two' do + assert_multiple_of_fourty_two 420 + end +end +``` + +or they can continue to be included directly into the relevant parent classes + +```ruby +# test/test_helper.rb +require 'test/sign_in_helper' + +class ActionDispatch::IntegrationTest + include SignInHelper +end +``` + +#### Eagerly Requiring Helpers + +You may find it convenient to eagerly require helpers in `test_helper.rb` so your test files have implicit access to them. This can be accomplished using globbing, as follows + +```ruby +# test/test_helper.rb +Dir[Rails.root.join('lib', 'test', '**', '*.rb')].each { |file| require file } +``` + +This has the downside of increasing the boot-up time, as opposed to manually requiring only the necessary files in your individual tests. + Testing Routes -------------- |