Rspec 未定义的局部变量或方法 root_path [英] Rspec undefined local variable or method root_path

查看:55
本文介绍了Rspec 未定义的局部变量或方法 root_path的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始使用 Rspec,但是当我运行 bundle exec rspec 时出现错误

I'm starting to use Rspec, but when I run bundle exec rspec I get an error

 /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,所以下面的问题不适用

I do not have Spork or Guard running so the question below shouldn't apply

未定义的局部变量或方法`root_path'(Rspec Spork Guard)

我在我的 spec_helper.rb 文件中添加了 config.include Rails.application.routes.url_helpers,但这没有帮助.未定义的局部变量或方法`root_path' Hartl 的教程第 5.3.2 章

I have added config.include Rails.application.routes.url_helpers in my spec_helper.rb file, but that did not help. undefined local variable or method `root_path' Hartl's Tutorial Chapter 5.3.2

这是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                                              

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

Update After reading about shared_examples, I tried this out successfully. Is there a better way to write this test? I ended up separating out the pages into individual pages like Home page etc.

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            

推荐答案

要保存您的结构 - 您可以像这样更改代码:

To save your structure - you can change the code like this:

require 'spec_helper'                                                                                                                                                       

describe "Pages" do                                                                                                                                                         
  describe "navigation" do                                                                                                                                                  

    shared_examples_for 'main page' do |path_name|                                                                                                                         
      before { visit send(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_behave_like 'main_page', :root_path                                                                                                                                               
    it_should_behave_like 'main_page', :faq_path                                                                                                                                                
    it_should_behave_like 'main_page', :about_path                                                                                                                                              
    it_should_behave_like 'main_page', :location_path                                                                                                                                           
    it_should_behave_like 'main_page', :contact_path                                                                                                                                            
    # it_should_behave_like 'main_page', :login_path                                                                                                                                            
  end                                                                                                                                                                       
end    

因为路径未在规范中的类级别定义"(c)您不能在规范类中调用路径方法.它应该在 it 块中.而且你的结构并不完美.如果您想避免重复,最好将代码放在模块中然后包含它.

because "paths are not defined at the class level in specs"(c) You cannot call path methods in spec class. It should be in it block. And your structure is not perfect. It will be better to put code in modules and then include it, if you want to avoid the duplication.

这篇关于Rspec 未定义的局部变量或方法 root_path的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆