URI::InvalidURIError 在 RSpec 水豚中执行功能测试时

URI::InvalidURIError when performing Feature Tests in RSpec Capybara

我目前正在 Capybara Rails 网站上为 Ruby 开发功能测试,每次我尝试 运行 这个测试文件时,我都会遇到同样的错误。 我在下面列出了用于测试的代码,收到的错误就在代码下方,并且仅缩短为主要事件。

require 'rails_helper'

RSpec.describe 'Home features' do
    it 'displays company name' do
        visit "/login"
        expect(page).to have_content("Company Name Here")
    end
end
  1) Home features displays company name
     Failure/Error: visit "/login"
     
     URI::InvalidURIError:
       bad URI(is not URI?): nil
     # /home/linuxuser/.rvm/gems/ruby-2.7.4/gems/capybara-3.36.0/lib/capybara/rack_test/browser.rb:76:in `build_uri'
     ...
     # ------------------
     # --- Caused by: ---
     # NoMethodError:
     #   undefined method `to_str' for nil:NilClass
     ...
     #   /home/linuxuser/.rvm/gems/ruby-2.7.4/gems/capybara-3.36.0/lib/capybara/rack_test/browser.rb:76:in `build_uri'

当然,我之前曾尝试自己调试它,因为这是一个 URI 问题,我指定了 URI gem 我最初认为这是一个 gem 冲突,当我删除时被证明是错误的它来自 Gemfile 并执行捆绑安装。

然后我测试了访问 url 的变体,包括单引号和双引号,还尝试了手动 uris,例如 www.google.com,最终出现了单独的获取错误,但是错误发生了在 uri 错误之前,所以我认为这不相关。

非常感谢有关此问题的任何帮助。

=============更新============= 这是 config/routes.rb

的片段
  match "/login", to: "sessions#new", via: [:get]
  match "/login", to: "sessions#create", via: [:post]

===进一步更新====== 添加了 StackTrace

FF

Failures:

  1) Home features displays company name
     Failure/Error: visit "/login"
     
     URI::InvalidURIError:
       bad URI(is not URI?): nil
     # /home/aus3r/.rvm/gems/ruby-2.7.4/gems/capybara-3.36.0/lib/capybara/rack_test/browser.rb:76:in `build_uri'
     # /home/aus3r/.rvm/gems/ruby-2.7.4/gems/capybara-3.36.0/lib/capybara/rack_test/browser.rb:68:in `process'
     # /home/aus3r/.rvm/gems/ruby-2.7.4/gems/capybara-3.36.0/lib/capybara/rack_test/browser.rb:56:in `block in process_and_follow_redirects'
     # /home/aus3r/.rvm/gems/ruby-2.7.4/gems/capybara-3.36.0/lib/capybara/rack_test/browser.rb:51:in `times'
     # /home/aus3r/.rvm/gems/ruby-2.7.4/gems/capybara-3.36.0/lib/capybara/rack_test/browser.rb:51:in `process_and_follow_redirects'
     # /home/aus3r/.rvm/gems/ruby-2.7.4/gems/capybara-3.36.0/lib/capybara/rack_test/browser.rb:24:in `visit'
     # /home/aus3r/.rvm/gems/ruby-2.7.4/gems/capybara-3.36.0/lib/capybara/rack_test/driver.rb:46:in `visit'
     # /home/aus3r/.rvm/gems/ruby-2.7.4/gems/capybara-3.36.0/lib/capybara/session.rb:278:in `visit'
     # /home/aus3r/.rvm/gems/ruby-2.7.4/gems/capybara-3.36.0/lib/capybara/dsl.rb:53:in `call'
     # /home/aus3r/.rvm/gems/ruby-2.7.4/gems/capybara-3.36.0/lib/capybara/dsl.rb:53:in `visit'
     # ./spec/features/home_spec.rb:5:in `block (2 levels) in <top (required)>'
     # ------------------
     # --- Caused by: ---
     # NoMethodError:
     #   undefined method `to_str' for nil:NilClass
     #   /home/aus3r/.rvm/gems/ruby-2.7.4/gems/capybara-3.36.0/lib/capybara/rack_test/browser.rb:76:in `build_uri'
    ```

在获得外部支持后,我意识到这是遗留代码和 https://github.com/biola/rack-cas (rack-cas gem) 的问题,并且需要将此添加到 config/application .rb

    config.rack_cas.exclude_paths = "/"

显然,由于现有遗留代码,该站点正在尝试执行 CAS 身份验证,这类似于@ThomasWalpole 和@smathy 提供的响应,因为他们提到该请求未经身份验证。