运行rspec测试时未初始化的常量,模型中使用的问题

时间:2015-05-06 16:15:21

标签: ruby-on-rails ruby rspec

当我运行rspec spec/models/football_match_spec.rb时出现uninitialized constant错误:

/Users/jamessmith/project/app/models/football_match.rb:3:in `<class:FootballMatch>': uninitialized constant FootballMatch::Type1Fixture (NameError)     
from /Users/jamessmith/project/app/models/football_match.rb:1:in `<top (required)>'     
from /Users/jamessmith/project/spec/models/football_match_spec.rb:3:in `<top (required)>'   
from /Users/jamessmith/.rvm/gems/ruby-2.1.2/gems/rspec-core-3.2.0/lib/rspec/core/configuration.rb:1226:in `load'    
from /Users/jamessmith/.rvm/gems/ruby-2.1.2/gems/rspec-core-3.2.0/lib/rspec/core/configuration.rb:1226:in `block in load_spec_files'    
from /Users/jamessmith/.rvm/gems/ruby-2.1.2/gems/rspec-core-3.2.0/lib/rspec/core/configuration.rb:1224:in `each'    
from /Users/jamessmith/.rvm/gems/ruby-2.1.2/gems/rspec-core-3.2.0/lib/rspec/core/configuration.rb:1224:in `load_spec_files'     
from /Users/jamessmith/.rvm/gems/ruby-2.1.2/gems/rspec-core-3.2.0/lib/rspec/core/runner.rb:97:in `setup'    
from /Users/jamessmith/.rvm/gems/ruby-2.1.2/gems/rspec-core-3.2.0/lib/rspec/core/runner.rb:85:in `run'  
from /Users/jamessmith/.rvm/gems/ruby-2.1.2/gems/rspec-core-3.2.0/lib/rspec/core/runner.rb:70:in `run'  
from /Users/jamessmith/.rvm/gems/ruby-2.1.2/gems/rspec-core-3.2.0/lib/rspec/core/runner.rb:38:in `invoke'   
from /Users/jamessmith/.rvm/gems/ruby-2.1.2/gems/rspec-core-3.2.0/exe/rspec:4:in `<top (required)>'     
from /Users/jamessmith/.rvm/gems/ruby-2.1.2/bin/rspec:23:in `load'  from /Users/jamessmith/.rvm/gems/ruby-2.1.2/bin/rspec:23:in `<main>'    
from /Users/jamessmith/.rvm/gems/ruby-2.1.2/bin/ruby_executable_hooks:15:in `eval'  
from /Users/jamessmith/.rvm/gems/ruby-2.1.2/bin/ruby_executable_hooks:15:in `<main>'

Type1Fixture关注:

module Type1Fixture
  extend ActiveSupport::Concern

  def competitors
    [competitor_1, competitor_2]
  end

  def competitor_1_score
    [fhhts || 0, shhts || 0, ethts || 0].reduce(:+)
  end

  def competitor_2_score
    [fhats || 0, shats || 0, etats || 0].reduce(:+)
  end

  def name_with_scores
    if [Fixture::IN_PROGRESS_STATUS, Fixture::COMPLETED_STATUS].include?(status)
      "#{competitor_1.name} #{home_team_score} - #{away_team_score} #{competitor_2.name}"
    else
      "#{competitor_1.name} vs #{competitor_2.name}"
    end
  end
end

编辑的FootballMatch模型:

class FootballMatch < Fixture
  include Mongoid::Document
  include Type1Fixture
end

我已将app/models/concerns添加到config.autoload_paths中的environments/test.rb数组中。

1 个答案:

答案 0 :(得分:1)

这可能是由于没有正确的文件名造成的。对于Type1Fixture,应该将包含该类的文件称为app/models/concerns/type1_fixture.rb。是这种情况吗?