简体   繁体   English

在 RSpec Capybara 中执行功能测试时出现 URI::InvalidURIError

[英]URI::InvalidURIError when performing Feature Tests in RSpec Capybara

I am currently developing feature tests for a Ruby on Rails website in Capybara and every time I attempt to run this test file, I am greeted with the same error.我目前正在 Capybara 中为 Ruby on Rails 网站开发功能测试,每次尝试运行此测试文件时,都会遇到同样的错误。 I have listed the code below for the test and the error received is just below the code and is shortened only to main events.我在下面列出了测试代码,收到的错误就在代码下面,并且只缩短为主要事件。

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'

I have of course attempted to debug this myself prior and given it was a URI issue and I specified the URI gem I originally thought it was a gem conflict which was disproved when I removed it from the Gemfile and performed a bundle install.我当然之前尝试过自己调试,因为这是一个 URI 问题,我指定了 URI gem,我最初认为这是一个 gem 冲突,当我从 Gemfile 中删除它并执行捆绑安装时,它被反驳了。

Afterwards I then tested variations of the visit url including single and double quotations as well as also trying manual uris such as www.google.com which ended up in a separate get error, however the error occurred prior to the uri error, so I assumed that was not related.之后,我测试了访问 url 的变体,包括单引号和双引号,还尝试了手动 uris,例如www.google.com ,最终导致单独的 get 错误,但是错误发生在 uri 错误之前,所以我假设那不相关。

Any help regarding this issue would be greatly appreciated thank you.非常感谢您提供有关此问题的任何帮助。

=============Update============= Here is a snippet from the config/routes.rb =============更新============= 这是来自 config/routes.rb 的片段

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

===Further Update ====== Added StackTrace ===进一步更新 ====== 添加了 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'
    ```

After receiving external support, I realised this was an issue with legacy code and the https://github.com/biola/rack-cas (rack-cas gem), and needed this addition into the config/application.rb在获得外部支持后,我意识到这是遗留代码和https://github.com/biola/rack-cas (rack-cas gem) 的问题,需要将此添加到 config/application.rb

    config.rack_cas.exclude_paths = "/"

Apparently the site was trying to perform CAS authentication due to existing legacy code which is similar to the responses provided by @ThomasWalpole and @smathy as they mentioned that the request was unauthenticated.显然,由于现有的遗留代码,该站点正在尝试执行 CAS 身份验证,这与@ThomasWalpole 和@smathy 提供的响应类似,因为他们提到请求未经身份验证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM