Ruby on rails &引用;NameError:错误的常量名称;将Rails 3.2升级到4.2之后

Ruby on rails &引用;NameError:错误的常量名称;将Rails 3.2升级到4.2之后,ruby-on-rails,ruby,ruby-on-rails-4,Ruby On Rails,Ruby,Ruby On Rails 4,我正在将一个Rails应用程序从3.2升级到4.2,在运行测试时,我得到了NameError:error constant name(这是完整的错误消息,没有确切说明它试图实现的内容)。使用--trace运行单个测试不提供任何附加信息。它似乎已经延伸到我的一个工厂(使用FactoryGirl,粘贴在下面),但工厂的定义对我来说很好 测试输出: $ bundle exec rake test TEST=test/unit/offer_test.rb --trace ** Invoke test (

我正在将一个Rails应用程序从3.2升级到4.2,在运行测试时,我得到了
NameError:error constant name
(这是完整的错误消息,没有确切说明它试图实现的内容)。使用
--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.
test/unit/offer\u 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

Rails 3.x使用了一个
test/unit/model\u test.rb
,而Rails 4.x使用了一个新的
test/models/model\u test.rb
,因此autoload可能无法找到类。检查两者之间的差异,这是我们的应用程序代码的一个问题。我们有一个
Category
模型,它有一个
#parent\u name
字段,还有一个
类别。parent\u name(child=nil)
类方法与Rails核心ext.方法冲突。

您的工厂没有定义
auction
属性,我想知道它是如何通过Rails3的。数据库中该字段有一个默认值,因此如果没有指定,它默认为
false
。为了子孙后代,我在工厂中添加了一个
auction
属性,并引发了相同的错误。啊,的确如此。然后,文件可能不再由rails自动加载,我记得Rails4中的自动加载路径已更改,但我不确定如何更改。你会尝试在测试中显式地
require\u relative
它吗?我们正在使用
factory\u girl\u rails
gem(最新版本),因此工厂定义正在加载。我重命名了目录以符合rails 4.x结构,但同样的问题也在发生。它似乎是在包含
FactoryGirl.create(…)
的行上,但我没有发现我的定义有任何错误。顺便说一句,我们的应用程序实际上既有单元测试也有rspec测试。我们正在慢慢地将所有单元测试转移到rspec。工厂定义位于
spec/factories
中,但在运行Rails 3.2时这不是问题,因为所有测试(单元和rspec)都通过了。请问您是如何发现问题的?我的
Comment
模型面临着几乎相同的问题。在您的情况下,它是否在尝试保存
类别
对象时给您提供了
名称错误:错误的常量名称
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