简体   繁体   中英

uninitialized constant when running rspec test with concerns used in models

When I run rspec spec/models/football_match_spec.rb I get an uninitialized constant error:

/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 concern:

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

Edited FootballMatch model:

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

I've added app/models/concerns to the config.autoload_paths array in environments/test.rb .

This can be caused by not having the correct file name. For Type1Fixture , the file containing the class should be called app/models/concerns/type1_fixture.rb . Is this the case?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM