You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: guides/source/testing.md
+50Lines changed: 50 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1397,6 +1397,56 @@ class ProfileControllerTest < ActionDispatch::IntegrationTest
1397
1397
end
1398
1398
```
1399
1399
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
0 commit comments