Ruby on rails 如何在Rails 5.2系统测试中填写或绕过HTTP基本身份验证?
我正在为一个在所有页面上都受http身份验证保护的站点编写一些测试 通过执行以下操作,我找到了一种绕过它进行控制器测试的方法Ruby on rails 如何在Rails 5.2系统测试中填写或绕过HTTP基本身份验证?,ruby-on-rails,automated-tests,capybara,Ruby On Rails,Automated Tests,Capybara,我正在为一个在所有页面上都受http身份验证保护的站点编写一些测试 通过执行以下操作,我找到了一种绕过它进行控制器测试的方法 get-tests\u-url,头:{'HTTP\u-AUTHORIZATION'=>ActionController::HttpAuthentication::Basic.encode\u-credentials('admin','admin')} 但是我如何通过系统测试呢?我已经搜索了好几个小时,试图找到一个解决方案,但我发现的所有建议似乎都是针对Rails/Capy
get-tests\u-url,头:{'HTTP\u-AUTHORIZATION'=>ActionController::HttpAuthentication::Basic.encode\u-credentials('admin','admin')}
但是我如何通过系统测试呢?我已经搜索了好几个小时,试图找到一个解决方案,但我发现的所有建议似乎都是针对Rails/Capybara的旧版本的,不起作用。Rails测试文档中似乎也没有任何与此相关的内容。好的,这对我来说似乎很有用
def visit_with_http_auth(path)
username = 'admin'
password = 'admin'
visit "http://#{username}:#{password}@#{Capybara.current_session.server.host}:#
{Capybara.current_session.server.port}#{path}"
end
然后在我的测试方法中,我只是
test "visiting the index" do
visit_with_http_auth questions_path
assert_selector "h1", text: "Questions"
end
好吧,这似乎对我有好处
def visit_with_http_auth(path)
username = 'admin'
password = 'admin'
visit "http://#{username}:#{password}@#{Capybara.current_session.server.host}:#
{Capybara.current_session.server.port}#{path}"
end
然后在我的测试方法中,我只是
test "visiting the index" do
visit_with_http_auth questions_path
assert_selector "h1", text: "Questions"
end
没必要惹水豚。只需将此方法添加到
test\u helper.rb
中的ActiveSupport::TestCase
:
def visit_authenticated(url)
uri = URI.parse(url)
uri.user = 'put_your_user_here'
uri.password = 'put_your_password_here'
visit uri
end
没必要惹水豚。只需将此方法添加到
test\u helper.rb
中的ActiveSupport::TestCase
:
def visit_authenticated(url)
uri = URI.parse(url)
uri.user = 'put_your_user_here'
uri.password = 'put_your_password_here'
visit uri
end
我编辑以将
Capybara.当前会话
替换为page
我编辑以将Capybara.当前会话
替换为page