I’m upgrading a Rails app from 3.2 to 4.2 and when running tests, I’m getting NameError: wrong constant name
(that’s the full error message, nothing saying exactly what it’s trying to constantize). Running a single test using --trace
provides no additional info. It does seem like it gets as far as one of my factories (using FactoryGirl, pasted below), but the factory definition looks fine to me.
Test output:
$ 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
Listings factory:
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
Answers
This ended up being an issue with our application code. We had a
Category
model that had a#parent_name
field, as well as aCategory.parent_name(child = nil)
class method that was conflicting with Rails core ext. Module#parent_name method.Rails 3.x used a
test/unit/model_test.rb
whereas rails 4.x uses a newtest/models/model_test.rb
so probably autoload is not finding the class because of that. Check the differences between the two here for 3.x and here for 4.x