The following method is a refactoring placed in test_helper.rb
def test_access(user, action)
sign_in user
get action
assert_response :success
end
in order to compact controller test statements
require "test_helper"
class CategorymajorsControllerTest < ActionDispatch::IntegrationTest
setup do
categorymajor = categorymajors(:one)
@categorymajor_params = { categorymajor: { name: 'some name' } }
@actions = [categorymajors_url, categorymajor_url(categorymajor)]
@translator_actions = [new_categorymajor_url, edit_categorymajor_url(categorymajor)]
end
test "should get index" do
@actions.each do |action|
@internal_users.each do |user|
test_access(user, action)
assert_response :success
end
end
end
test "should get new edit" do
@translator_actions.each do |action|
test_access(@admin, action)
assert_response :success
end
end
end
If the test is run per individual test
Finished in 0.831613s, 1.2025 runs/s, 14.4298 assertions/s.
1 runs, 12 assertions, 0 failures, 0 errors, 0 skips
but if the entire test controller is launched, two errors arise; the first everytime the method is called, the second once.
Error:
ActionDispatch::IntegrationTest#test_access:
ArgumentError: wrong number of arguments (given 0, expected 2)
test/test_helper.rb:51:in `test_access'
[...]
Rails::Generators::TestCase#test_access:
RuntimeError: You need to configure your Rails::Generators::TestCase destination root.
/Users/jerdvo/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/railties-7.1.3.2/lib/rails/generators/testing/behavior.rb:91:in `destination_root_is_set?'
/Users/jerdvo/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/railties-7.1.3.2/lib/rails/generators/testing/setup_and_teardown.rb:8:in `setup'
This makes little sense for two reasons:
a) two arguments are given, thus the call is somehow diverted from an expected path.
b) why would an individual test run (and pass) while a group, no.
The documentation does not tell me where that file should be saved.
What is going on / error ?
The reason for both errors is because the method in the test_helper.rb was not under
private, explaining whytest_accessmethod was getting 0 arguments.Proper:
Rails::Generators::TestCaseremains opaque to this reader.