Rspec and named routes

前端 未结 6 1474
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-30 06:49

I\'m quite new to rails, and trying to follow the railstutorial. Everything goes fine, except for my tests which can\'t get past the named routes (5.3.3)

My routes.r

相关标签:
6条回答
  • 2020-11-30 06:57

    Named routes should work if you put the following in rails_helper.rb not the spec_helper.rb:

    checkout at my rails_helper.rb code

    
    # This file is copied to spec/ when you run 'rails generate rspec:install'
    require 'spec_helper'
    ENV['RAILS_ENV'] ||= 'test'
    require File.expand_path('../config/environment', __dir__)
    # Prevent database truncation if the environment is production
    if Rails.env.production?
      abort('The Rails environment is running in production mode!')
    end
    require 'rspec/rails'
    require 'capybara/rails'
    RSpec.configure do |config|
      config.include Rails.application.routes.url_helpers
      config.include Devise::Test::ControllerHelpers, type: :controller
      config.include Devise::Test::ControllerHelpers, type: :view
      config.include Warden::Test::Helpers
    end
    
    begin
      ActiveRecord::Migration.maintain_test_schema!
    rescue ActiveRecord::PendingMigrationError => e
      puts e.to_s.strip
      exit 1
    end
    RSpec.configure do |config|
      # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
      config.fixture_path = "#{::Rails.root}/spec/fixtures"
    
      config.use_transactional_fixtures = true
    
      config.infer_spec_type_from_file_location!
    
      # Filter lines from Rails gems in backtraces.
      config.filter_rails_from_backtrace!
    end
    
    
    0 讨论(0)
  • 2020-11-30 07:04

    Named routes should work if you put the following in rspec_helper.rb:

    RSpec.configure do |config|
      config.include Rails.application.routes.url_helpers
      ...
    end
    

    Is that how you set it up?

    0 讨论(0)
  • 2020-11-30 07:08

    You should have used

    rails generate rspec:install
    

    instead of

    rspec --init
    

    and you wouldn't have had to modify the config file.

    Don't do it now though or your application will break and you'll have to waste some more time figuring out why it broke.

    0 讨论(0)
  • 2020-11-30 07:12

    I don't think you have access to named routes inside of your rspec controller specs. You could however just do visit('/'), which is the equivalent of root_path.

    0 讨论(0)
  • 2020-11-30 07:12

    Google brought me here, even my error message doesn't fit 100%.

    In my case Capybara command visitis unknown...

    Error:

    NoMethodError:
           undefined method `visit' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0xa49a73c>
    

    Since Capybara 2.0 one has to use folder spec/features capybara commands don't work in folder spec/requests anymore.

    That helped me: http://alindeman.github.com/2012/11/11/rspec-rails-and-capybara-2.0-what-you-need-to-know.html

    Hope you find this usefull.

    0 讨论(0)
  • 2020-11-30 07:15

    I had the same problem, with the same Tutorial. It turns out that I needed to just restart the Spork service, and all worked fine.

    The solution posted by Tom L worked for me, but when I removed that line and restarted Spork, that also fixed the problem.

    Hope that helps out any other people who are wary about deviating from the tutorial's code!

    0 讨论(0)
提交回复
热议问题