【问题标题】:Rspec and named routesRspec 和命名路由
【发布时间】:2012-02-28 03:06:22
【问题描述】:

我对 Rails 很陌生,并试图遵循 railstutorial。一切都很好,除了我的测试无法通过命名路线(5.3.3)

我的路线.rb:

 SampleApp::Application.routes.draw do

 resources :users
 match '/signup',  to: 'users#new'

 match '/help',    to: 'static_pages#help'
 match '/about',   to: 'static_pages#about'
 match '/contact', to: 'pages#contact'

 root to: 'static_pages#home'

 #Commented stuff

我的第一次测试(spec/controllers/static_pages_controller_spec.rb):

describe "Static pages" do

subject { page }

shared_examples_for "all static pages" do
  it { should have_selector('h1',    text: heading) }
  it { should have_selector('title', text: full_title(page_title)) }
end

describe "Home page" do
  before { visit root_path }
  let(:heading)    { 'Sample App' }
  let(:page_title) { 'Home' }

  it_should_behave_like "all static pages"
end

#Other tests

spec_helper.rb 看起来像(没有所有注释的东西)

ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'

Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

RSpec.configure do |config|
    config.fixture_path = "#{::Rails.root}/spec/fixtures"
    config.use_transactional_fixtures = true
    config.infer_base_class_for_anonymous_controllers = false
end

我从 rspec 得到的错误都是这样的:

 Static pages Home page it should behave like all static pages 
 Failure/Error: before { visit root_path }
 NameError:
   undefined local variable or method `root_path' for #<RSpec::Core::ExampleGroup::Nested_2::Nested_1::Nested_1:0x00000004a12210>
 Shared Example Group: "all static pages" called from ./spec/controllers/static_pages_controller_spec.rb:17
 # ./spec/controllers/static_pages_controller_spec.rb:13:in `block (3 levels) in <top (required)>'

我已经尝试过使用

 include Rails.application.routes.url_helpers

在 spec_helper 中,但它将我的错误更改为

 Static pages Home page it should behave like all static pages 
 Failure/Error: Unable to find matching line from backtrace
 SystemStackError:
   stack level too deep
 # /usr/lib/ruby/1.9.1/forwardable.rb:185

我也尝试了不同的方法来重命名我的路线,但都没有奏效。我又回到教程版了。

如果它对找出问题所在有任何帮助,我使用的是 Ubuntu 11.10,带有 rails 3.2.1 和 ruby​​ 1.9.2p290。希望你能帮忙,我花了很长时间在谷歌上搜索解决方案,但没有找到任何解决方案^^'

【问题讨论】:

    标签: ruby-on-rails railstutorial.org


    【解决方案1】:

    如果您将以下内容放入 rspec_helper.rb 中,命名路由应该可以工作:

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

    你是这样设置的吗?

    【讨论】:

    • 这正是我所做的,除了“配置”。部分。现在一切正常!非常感谢!
    • 万一有人做到了这一步但仍然被难住了,还值得注意的是 root_path (etc) 只在 "it" 部分的 "do...end" 块内工作 "描述”RSpec 测试的块。因此,如果您根据传递给函数的路径参数创建函数来处理“it”部分,则不能将 root_path (等)作为参数传递给所述函数。
    • 这也解决了我的 Rails 4.2 弃用警告问题 DEPRECATION WARNING: named_routes.helpers is deprecated, please use route_defined?(route_name) to see if a named route was defined 所以现在我有干净的 rspec 日志,直到我等待 github.com/rspec/rspec-rails/pull/1142 被释放。
    • 我想你的意思是rails_helper.rb
    【解决方案2】:

    我认为您无法访问 rspec 控制器规范中的命名路由。但是,您可以只做 visit('/'),​​这相当于 root_path。

    【讨论】:

    • 谢谢,它运行良好,让我的测试变得绿色!但是,本教程似乎使用命名路由railstutorial.org,我很想知道如何^^
    • 欢迎您投票并接受答案! :)
    • 点赞需要 15 声望,对不起!我会在接受之前稍等片刻,以防有人真的知道如何让它像书中一样工作:)
    【解决方案3】:

    Google 把我带到了这里,即使我的错误信息也不是 100%。

    就我而言,Capybara 命令visit未知...

    错误:

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

    由于 Capybara 2.0 必须使用文件夹 spec/features capybara 命令不再在文件夹 spec/requests 中工作。

    这对我有帮助: http://alindeman.github.com/2012/11/11/rspec-rails-and-capybara-2.0-what-you-need-to-know.html

    希望你觉得这很有用。

    【讨论】:

      【解决方案4】:

      我有同样的问题,使用相同的教程。事实证明,我只需要重新启动 Spork 服务,一切正常。

      Tom L 发布的解决方案对我有用,但是当我删除该行并重新启动 Spork 时,这也解决了问题。

      希望对那些担心偏离教程代码的人有所帮助!

      【讨论】:

      • 非常感谢! @Tom L 的代码对我不起作用。所以这毕竟是Spork的错!我整个下午都在寻找解决方案!非常感谢!
      【解决方案5】:

      你应该用过

      rails generate rspec:install
      

      而不是

      rspec --init
      

      而且您不必修改配置文件。

      但现在不要这样做,否则您的应用程序将崩溃,您将不得不浪费更多时间来找出它崩溃的原因。

      【讨论】:

      • 但是您的评论确实不是解决方案。 ;-)
      【解决方案6】:

      如果您将以下内容放入 rails_helper.rb 而不是 spec_helper.rb,则命名路由应该可以工作:

      在我的 rails_helper.rb 代码中结帐

      
      # 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
      
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-12-18
        • 1970-01-01
        • 1970-01-01
        • 2011-12-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-06-14
        相关资源
        最近更新 更多