Rspec 未定义的局部变量或方法 root_path

2023-12-30

我开始使用 Rspec,但是当我运行时bundle exec rspec我收到一个错误

 /spec/requests/pages_spec.rb:20:in `block (2 levels) in <top (required)>': undefined local
 variable or method `root_path' for #<Class:0x00000102e46850> (NameError)

我没有运行 Spork 或 Guard,因此以下问题不适用

未定义的局部变量或方法“root_path”(Rspec Spork Guard) https://stackoverflow.com/questions/14871918/undefined-local-variable-or-method-root-path-rspec-spork-guard

我已经添加了config.include Rails.application.routes.url_helpers in my spec_helper.rb文件,但这没有帮助。未定义的局部变量或方法“root_path”Hartl 的教程第 5.3.2 章 https://stackoverflow.com/questions/12240868/undefined-local-variable-or-method-root-path-hartls-tutorial-chapter-5-3-2

Here's pages_spec.rb

require 'spec_helper'                                                                                                                                                       

describe "Pages" do                                                                                                                                                         
  describe "navigation" do                                                                                                                                                  

    def self.it_should_be_on(path_name, value=nil)                                                                                                                          
      before { visit path_name }                                                                                                                                            

      it "should be on #{path_name}" do                                                                                                                                     
        page.should have_link('Home')                                                                                                                                       
        page.should have_link('Inventory')                                                                                                                                  
        page.should have_link('FAQ')                                                                                                                                        
        page.should have_link('About Us')                                                                                                                                   
        page.should have_link('Location')                                                                                                                                   
        page.should have_link('Contact Us')                                                                                                                                 
        # page.should have_link('Login')                                                                                                                                    
      end                                                                                                                                                                   
    end                                                                                                                                                                     

    it_should_be_on root_path                                                                                                                                               
    it_should_be_on faq_path                                                                                                                                                
    it_should_be_on about_path                                                                                                                                              
    it_should_be_on location_path                                                                                                                                           
    it_should_be_on contact_path                                                                                                                                            
    # it_should_be_on login_path                                                                                                                                            
  end                                                                                                                                                                       
end       

spec_helper.rb

# This file is copied to spec/ when you run 'rails generate rspec:install'                                                                                                  
ENV["RAILS_ENV"] ||= 'test'                                                                                                                                                 
require File.expand_path("../../config/environment", __FILE__)                                                                                                              
require 'rspec/rails'                                                                                                                                                       
require 'rspec/autorun'                                                                                                                                                     
# Requires supporting ruby files with custom matchers and macros, etc,                                                                                                      
# in spec/support/ and its subdirectories.                                                                                                                                  
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }                                                                                                         

# Checks for pending migrations before tests are run.                                                                                                                       
# If you are not using ActiveRecord, you can remove this line.                                                                                                              
ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)                                                                                                 

RSpec.configure do |config|                                                                                                                                                 
  # ## Mock Framework                                                                                                                                                       
  #                                                                                                                                                                         
  # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:                                                                                             
  #                                                                                                                                                                         
  # config.mock_with :mocha                                                                                                                                                 
  # config.mock_with :flexmock                                                                                                                                              
  # config.mock_with :rr                                                                                                                                                    

  # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures                                                                                              
  config.fixture_path = "#{::Rails.root}/spec/fixtures"                                                                                                                     

  # If you're not using ActiveRecord, or you'd prefer not to run each of your                                                                                               
  # examples within a transaction, remove the following line or assign false                                                                                                
  # instead of true.                                                                                                                                                        
  config.use_transactional_fixtures = true                                                                                                                                  

  # If true, the base class of anonymous controllers will be inferred                                                                                                       
  # automatically. This will be the default behavior in future versions of                                                                                                  
  # rspec-rails.                                                                                                                                                            
  config.infer_base_class_for_anonymous_controllers = false                                                                                                                 

  # Run specs in random order to surface order dependencies. If you find an                                                                                                 
  # order dependency and want to debug it, you can fix the order by providing                                                                                               
  # the seed, which is printed after each run.                                                                                                                              
  #     --seed 1234                                                                                                                                                         
  config.order = "random"                                                                                                                                                   
  config.include Capybara::DSL                                                                                                                                              
  config.include Rails.application.routes.url_helpers                                                                                                                       
end                                              

Update在阅读了shared_examples之后,我成功地尝试了这个。有没有更好的方法来编写这个测试?我最终将页面分成单独的页面,例如主页等。

require 'spec_helper'                                                               

describe "Pages" do                                                                 

  subject { page }                                                                  

  shared_examples "navigation" do |path_name|                                       
    before { visit send( path_name) }                                               

    describe "navigation links should be on #{path_name}" do                        

      it { should have_link('Home') }                                               
      it { should have_link('Inventory') }                                          
      it { should have_link('FAQ') }                                                
      it { should have_link('About Us') }                                           
      it { should have_link('Location') }                                           
      it { should have_link('Contact Us') }                                         
      # it { should have_link('Login') }                                            
    end                                                                             
  end                                                                               

  describe "Home Page" do                                                           
    include_examples "navigation", :root_path                                       
  end                                                                               
end            

RSpec 的顶层没有可用的 Rails 助手describe堵塞。它们仅在较低级别的块中可用(例如let, before, it, etc.).

如果要在示例之间共享此类代码,可以使用共享上下文或共享示例,如 RSpec 文档中所述,或者切换到使用符号作为参数describelevel 并推迟将它们解释为方法,直到您处于较低级别的块中,如下所示@IharDrozdov 的答案 https://stackoverflow.com/a/21211114/1008891.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Rspec 未定义的局部变量或方法 root_path 的相关文章

随机推荐