Home > Mobile >  URI::InvalidURIError when performing Feature Tests in RSpec Capybara
URI::InvalidURIError when performing Feature Tests in RSpec Capybara

Time:05-18

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. 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.

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.

Any help regarding this issue would be greatly appreciated thank you.

=============Update============= Here is a snippet from the config/routes.rb

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

===Further Update ====== Added 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'
    ```

CodePudding user response:

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

    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.

  • Related