M
M
mlwrm2017-01-06 19:56:17
Ruby on Rails
mlwrm, 2017-01-06 19:56:17

Why don't url helpers work in tests?

There is an elementary test

require 'rails_helper'

RSpec.describe "Test if root exists", type: :request do
  describe "GET /" do
    it "visits root" do
      visit root_url
      expect(current_url).to eql(root_url)
    end
  end
end

When I run I get an error
1) Test if root exists GET / visits root
Failure/Error: visit root_url
NoMethodError:
undefined method `empty?' for nil:NilClass
# ./spec/requests/root_spec.rb:6:in `block (3 levels) in '

spec/spec_helper.rb
RSpec.configure do |config|
  config.expect_with :rspec do |expectations|
    expectations.include_chain_clauses_in_custom_matcher_descriptions = true
  end

  config.mock_with :rspec do |mocks|
    mocks.verify_partial_doubles = true
  end
end

spec/rails_helper.rb
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'spec_helper'
require 'rspec/rails'
require 'capybara/rspec'
require 'factory_girl_rails'

Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }
ActiveRecord::Migration.maintain_test_schema!

RSpec.configure do |config|
  config.include Rails.application.routes.url_helpers
  config.infer_spec_type_from_file_location!
  config.before(:suite) { DatabaseCleaner.clean_with :truncation }
  config.before(:each) { DatabaseCleaner.strategy = :transaction }
  config.before(:each, js: true) do
    DatabaseCleaner.strategy = :truncation
  end
  config.before(:each) { DatabaseCleaner.start }
  config.after(:each) { DatabaseCleaner.clean }
  config.filter_rails_from_backtrace!
end

The error occurs because root_url tried to just display its value, I still get an error.
Tell me what's the problem?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
mlwrm, 2017-01-10
@malworm

Solution
stackoverflow.com/questions/41510195/why-url-heple...

A
Anton Dyachuk, 2017-01-09
@Renius

If you enter a root_url value like '\' and the error persists, then it is not a root_url error.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question