RubyonRails教程中的问题,需要帮助澄清想法



在RoR教程中,

test/test_helper.rb

...
class ActiveSupport::TestCase
# Run tests in parallel with specified workers
parallelize(workers: :number_of_processors)
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all
include ApplicationHelper
# Returns true if a test user is logged in.
def is_logged_in?
!session[:user_id].nil?
end
...

清楚地表明;is_loged_in"是在类ActiveSupport::TestCase中定义的,然而,ActionDispatch类的集成测试怎么可能找到它呢?如图所示:

test/integration/user_signup_test.rb

require 'test_helper'
class UsersSignupTest < ActionDispatch::IntegrationTest
...
test "valid signup information with account activation" do
...
log_in_as(user)
assert_not is_logged_in?
# Invalid activation token
get edit_account_activation_path("invalid token", email: user.email)
assert_not is_logged_in?
# Valid token, wrong email
get edit_account_activation_path(user.activation_token, email: 'wrong')
assert_not is_logged_in?
# Valid activation token
get edit_account_activation_path(user.activation_token, email: user.email)
assert user.reload.activated?
follow_redirect!
assert_template "users/show"
assert is_logged_in?
end
end

根据我的理解,除非;is_loged_in"在类ActionDispatch中定义,它应该抛出类似"的错误;nomethoderror,未定义的is_loged_in&";,然而,为什么不是这样呢?

ActionDispatch::IntegrationTestActiveSupport::TestCase继承了许多其他内容,因此在ActiveSupport::TestCase中进行的任何修改也将在子类中可用。

请理解,在test_helper.rb中,class ActiveSupport::TestCase不会创建新类,而是重新打开现有类以添加一些更改。

最新更新