" NameError:错误的常量名称"将Rails 3.2升级到4.2后

时间:2017-04-21 16:10:09

标签: ruby-on-rails ruby ruby-on-rails-4

我正在将Rails应用从3.2升级到4.2,在运行测试时,我收到了NameError: wrong constant name(这是完整的错误消息,没有任何内容可以说是什么&#39} ; s试图进行constantize)。使用--trace运行单个测试不会提供其他信息。它看起来像是我的一个工厂(使用FactoryGirl,粘贴在下面),但工厂定义对我来说很好。

测试输出:

$ bundle exec rake test TEST=test/unit/offer_test.rb --trace
** Invoke test (first_time)
** Execute test
** Invoke test:single (first_time)
** Invoke test:prepare (first_time)
** Execute test:prepare
** Execute test:single
DEPRECATION WARNING: Core extensions are deprecated and will be removed in Squeel 2.0. (called from /path/to/app/config/initializers/squeel.rb:11:in `block in <top (required)>')
DEPRECATION WARNING: [Devise] including `Devise::TestHelpers` is deprecated and will be removed from Devise.
For controller tests, please include `Devise::Test::ControllerHelpers` instead.
 (called from include at /path/to/app/test/test_helper.rb:15)
DEPRECATION WARNING: [Devise] including `Devise::TestHelpers` is deprecated and will be removed from Devise.
For controller tests, please include `Devise::Test::ControllerHelpers` instead.
 (called from include at /path/to/app/test/test_helper.rb:23)
Run options: --seed 32894

# Running:

EEE

Finished in 0.445273s, 6.7374 runs/s, 0.0000 assertions/s.

  1) Error:
OfferTest#test_should_know_if_accepted_or_not:
NameError: wrong constant name 
    test/unit/offer_test.rb:8:in `setup'


  2) Error:
OfferTest#test_should_not_be_able_to_create_offer_on_ended_listing:
NameError: wrong constant name 
    test/unit/offer_test.rb:8:in `setup'


  3) Error:
OfferTest#test_should_create_new_from_hash_and_properly_set_all_attributes:
NameError: wrong constant name 
    test/unit/offer_test.rb:8:in `setup'

3 runs, 0 assertions, 0 failures, 3 errors, 0 skips
Coverage report generated for Unit Tests to /path/to/app/coverage. 1540 / 21634 LOC (7.12%) covered.

的Gemfile:

source 'http://rubygems.org'

ruby '2.1.8'

gem 'rails', '4.2.8'
gem 'rake', '0.9.2.2'

# Gems used only for assets and not required in production environments by default.
gem 'sass-rails', "~> 5.0.6"
gem 'coffee-rails', "~> 4.2.1"
gem 'uglifier', ">= 1.0.3"

# Backwards compat gems used when upgrading from Rails 3 to 4.
gem 'protected_attributes', '~> 1.1.3'
gem 'activerecord-deprecated_finders', '~> 1.0.4', require: 'active_record/deprecated_finders'

gem 'json', '~> 1.8.6'
gem 'pg'
gem 'unicorn'
gem 'jquery-rails'
gem 'slim', '~> 3.0.7' # HTML templating
gem 'devise', '~> 4.2.1' # Authentication
gem 'sidekiq', '~> 4.2.10'
gem 'sidekiq-middleware', '~> 0.3.0'
gem 'sidekiq_status', '~> 1.2.0'
gem 'sinatra', :require => false # For Sidekiq monitoring
gem 'dalli' # memcached used for Rails.cache and sessions
gem 'kgio' # Nonblocking I/O for Ruby - performance boost to dalli
gem 'fog', '~> 1.40.0' # Storage on S3 for carrierwave
gem 'fog-aws', '~> 1.3.0'
gem 'carrierwave', '~> 1.0.0'
gem 'mini_magick'
gem 'filepicker-rails', '~> 2.1.0' # Image uploading
gem 'squeel', '~> 1.2.3'
gem 'stripe', '~> 2.4.0' # Payments
gem 'kaminari', '~> 1.0.1'
gem 'setler' # Settings
gem 'utf8-cleaner' # Rack middleware for cleaing up URL params
gem 'rakismet' # Filtering for spam via akismet.com
gem 'sanitize' # Add attributes like nofollow/target to anchors (per initializer config)
gem 'geokit-rails' # old free geocoding
gem 'geocoder' # new free geocoding
gem 'airbrake', '~> 6.0' # Error notifications
gem 'ancestry', '~> 2.2.2'
gem 'easy_money'
gem 'easypost', '~> 2.7.0' # Shipping
gem 'sift', '~> 2.1.0.1' # Fraud prevention
gem 'paypal-sdk-adaptivepayments' # Used for split payments.
gem 'paypal-sdk-adaptiveaccounts', :require => false # Verify PayPal email
gem 'paypal-sdk-rest', :require => false # REST API client
gem 'friendly_id'
gem 'font_assets'
gem 'griddler', '~> 1.4.0' # incoming emails for conversations
gem 'griddler-sendgrid', '~> 1.0.0'
gem 'aws-sdk', '~> 2.9.9'
gem 'text'
gem 'mailhopper'
gem 'audited-activerecord', '~> 4.2.2'
gem 'acts_as_list', '~> 0.9.5' # Used for sorting images
gem 'newrelic_rpm', '~> 4.1.0.333'
gem 'httpclient'
gem 'httparty'
gem 'rest-client', '~> 2.0.1' # for handling POST's to API images
gem 'hipchat'
gem 'musicbrainz' # Used to fetch album art.
gem 'bourbon', '~> 4.3.4' # Sass mixins libraries
gem 'armor_payments' # Escrow
gem 'countries', require: 'countries/global'
gem 'koala' # Facebook API tools
gem 'twilio-ruby' # Twilio text-messaging API
gem 'createsend' # Campaign Monitor integration
gem 'oink' # memory usage logger
gem 'browser', '~> 2.3.0' # used to detect mobile user-agent
gem 'redis-namespace', '~> 1.5.3'

group :development do
  gem 'brakeman', :require => false
  gem 'pry-rails'
  gem 'redcarpet' # for YARD doc markdown formatting
  gem 'yard'
  gem 'rails-footnotes'
end

group :test do
  gem 'database_cleaner'
  gem 'email_spec'
  gem 'minitest', '~> 5.10.1'
  gem 'mocha', :require => false # Mocha needs to be required after the env is set up. See test/test_helper.rb
  gem 'shoulda-matchers'
  gem 'simplecov', :require => false
  gem 'stripe-ruby-mock' # Mocking Stripe requests
  gem 'webmock' # stubbing HTTP requests
end

group :development, :test do
  gem 'byebug' # Debugger for Ruby >= 2.0
  gem 'foreman'
  gem 'forgery' # For randomly generating seed data in dev environment
  gem 'factory_girl_rails', '~> 4.8.0' # Used mostly in tests
  gem 'rspec', '~> 3.5.0'
  gem 'rspec-rails', '~> 3.5.2'
  gem 'faraday' # HTTP stubbing for vcr
  # gem 'mailcatcher' # Capture and display local mailers. NOTE: Recommended to not be in Gemfile: https://github.com/sj26/mailcatcher#bundler
  gem 'net-http-spy', :require => false
  # gem 'rack-mini-profiler' # Displays a speed badge for every html page
  gem 'vcr' # 'Playback' canned HTTP responses when testing

  # Generate Database Diagram; Must install Graphviz to use locally (`brew install graphviz`, etc)
  # gem 'rails-erd'
  # rake db_diagram
end

test/unit/offer_test.rb

require 'test_helper'

class OfferTest < ActiveSupport::TestCase

  def setup
    @buyer = FactoryGirl.create(:user, username: 'test_buyer')
    @seller = FactoryGirl.create(:user, username: 'test_seller')
    @listing = FactoryGirl.create(:listing, auction: false)  # <-- This is line 8, where the test throws the NameError.
    @offer = FactoryGirl.create(:offer, buyer: @buyer, listing: @listing)
  end

  # ...snip...
end

上市工厂:

FactoryGirl.define do
  factory :listing do |l|
    l.association :category
    l.association :seller, factory: :user

    l.age { [3, 3, 9, 9, 10].sample }
    l.album_art_url { media.present? ? "http://example.com/images/#{rand(1000)}.png" : nil }
    l.album_art_back_url { media.present? ? "http://example.com/images/#{rand(1000)}-back.png" : nil }
    l.auction_opening_bid { Forgery(:monetary).formatted_money }
    l.auction_reserve_price { Forgery(:monetary).formatted_money }
    l.bold { (rand(2) == 0) }
    l.box_count 1
    l.brand { Forgery(:name).company_name }
    l.classified_asking_price_dollars { Forgery(:monetary).formatted_money(min: 1) }
    l.condition { rand(10) + 1 }
    l.description { Forgery(:lorem_ipsum).sentences(20) }
    l.hit_count { Forgery(:basic).number(at_most: 69) }
    l.media false
    l.product_name { Forgery(:basic).text }
    l.no_transaction_fee false
    l.payment_methods ["PERS", "DISC"]
    l.retail_price_dollars { Forgery(:monetary).formatted_money }
    l.ship_from "29601"
    l.ship_to "US"
    l.shipping_methods ["UPS", "FDX"]
    l.status Listing::STATUSES[:active]
    l.title { Forgery(:basic).text }
    l.auction_duration 7
  end

  factory :listing_with_shipping_info, parent: :listing do |l|
    after(:create) do |listing|
      address = FactoryGirl.create(:address, user: listing.seller)
      listing.shipment.update_attributes(seller_address: address)
    end
  end
end

2 个答案:

答案 0 :(得分:0)

Rails 3.x使用了test/unit/model_test.rb而rails 4.x使用了新的test/models/model_test.rb,因此自动加载可能因为这一点而没有找到类。检查两个here for 3.xhere for 4.x

之间的差异

答案 1 :(得分:0)

这最终成为我们的应用程序代码的问题。我们有一个Category模型,其中包含#parent_name字段,以及与Rails核心扩展冲突的Category.parent_name(child = nil)类方法。 Module#parent_name方法。