Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何在Rails 5.2系统测试中填写或绕过HTTP基本身份验证?_Ruby On Rails_Automated Tests_Capybara - Fatal编程技术网

Ruby on rails 如何在Rails 5.2系统测试中填写或绕过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

我正在为一个在所有页面上都受http身份验证保护的站点编写一些测试

通过执行以下操作,我找到了一种绕过它进行控制器测试的方法

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