Skip to content

Commit d45b74e

Browse files
committed
Add advanced test helpers docs to guides
[ci skip]
1 parent 1decfed commit d45b74e

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

guides/source/testing.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1397,6 +1397,56 @@ class ProfileControllerTest < ActionDispatch::IntegrationTest
13971397
end
13981398
```
13991399

1400+
#### Using Separate Files
1401+
1402+
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`.
1403+
1404+
```ruby
1405+
# lib/test/multiple_assertions.rb
1406+
module MultipleAssertions
1407+
def assert_multiple_of_fourty_two(number)
1408+
assert (number % 42 == 0), 'expected #{number} to be a multiple of 42'
1409+
end
1410+
end
1411+
```
1412+
1413+
These helpers can then be explicitly required as needed and included as needed
1414+
1415+
```ruby
1416+
require 'test_helper'
1417+
require 'test/multiple_assertions'
1418+
1419+
class NumberTest < ActiveSupport::TestCase
1420+
include MultipleAssertions
1421+
1422+
test '420 is a multiple of fourty two' do
1423+
assert_multiple_of_fourty_two 420
1424+
end
1425+
end
1426+
```
1427+
1428+
or they can continue to be included directly into the relevant parent classes
1429+
1430+
```ruby
1431+
# test/test_helper.rb
1432+
require 'test/sign_in_helper'
1433+
1434+
class ActionDispatch::IntegrationTest
1435+
include SignInHelper
1436+
end
1437+
```
1438+
1439+
#### Eagerly Requiring Helpers
1440+
1441+
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
1442+
1443+
```ruby
1444+
# test/test_helper.rb
1445+
Dir[Rails.root.join('lib', 'test', '**', '*.rb')].each { |file| require file }
1446+
```
1447+
1448+
This has the downside of increasing the boot-up time, as opposed to manually requiring only the necessary files in your individual tests.
1449+
14001450
Testing Routes
14011451
--------------
14021452

0 commit comments

Comments
 (0)