Rails 3.2 - 未初始化的常量FactoryGirl(名称错误)

时间:2015-07-23 15:45:42

标签: ruby-on-rails rspec factory-bot

我运行命令 rake test:prepare 并显示错误:

NameError:未初始化的常量FactoryGirl /home/dima/myapp/bdsmgalaxy/spec/factories/polls.rb:3:in <top (required)>' /home/dima/myapp/bdsmgalaxy/config/environment.rb:5:in' 任务:TOP =&gt; test:prepare =&gt; db:test:prepare =&gt; db:abort_if_pending_migrations =&gt;环境 (通过使用--trace运行任务查看完整跟踪)

我有 Rails版本3.2

我的设置是rspec和工厂

工厂/ polls.rb

# Read about factories at http://github.com/thoughtbot/factory_girl

FactoryGirl.define do

  factory :poll_item do
    answer "Ruby"
  end

  factory :poll do
    question "What programming language are you using?"
    results_hidden 1
    from_date "2015-06-25 18:13:18"
    to_date "2015-07-25 18:13:18"

    trait :poll_item1 do
      association :poll_item, answer: "C#"
    end

    trait :poll_item2 do
      association :poll_item, answer: "Ruby"
    end

    factory :poll_with_item1, traits: [:poll_item1, :poll_item2]
    factory :poll_with_item2, traits: [:poll_item2]
  end
end

的Gemfile:

group :development, :test do
  gem 'rspec-rails',        '2.5.0'
  gem 'factory_girl',       '1.3.3'
  gem 'factory_girl_rails', '1.0.1'
  gem 'rails3-generators',  '0.17.4'
  gem 'timecop',            '0.3.5'
  gem 'quiet_assets'
end

spec_helper.rb:

# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'authlogic/test_case'
require 'factory_girl_rails'


# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

RSpec.configure do |config|
  config.include FactoryGirl::Syntax::Methods

  # == Mock Framework
  #
  # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
  #
  # config.mock_with :mocha
  # config.mock_with :flexmock
  # config.mock_with :rr
  config.mock_with :rspec

  # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
  config.fixture_path = "#{::Rails.root}/spec/fixtures"

  # If you're not using ActiveRecord, or you'd prefer not to run each of your
  # examples within a transaction, remove the following line or assign false
  # instead of true.
  config.use_transactional_fixtures = true

  config.include Authlogic::TestCase


end

application.rb中:

require File.expand_path('../boot', __FILE__)

require 'rails/all'

if defined?(Bundler)
  # If you precompile assets before deploying to production, use this line
  Bundler.require(*Rails.groups(:assets => %w(development test)))
  # If you want your assets lazily compiled in production, use this line
  # Bundler.require(:default, :assets, Rails.env)
end

module Bdsmgalaxy
  class Application < Rails::Application
    config.autoload_paths += %W(#{config.root}/lib)
    config.autoload_paths += %W(#{config.root}/app/workers)
    config.autoload_paths += Dir["#{config.root}/lib/**/"]
    config.time_zone = 'Moscow'
    config.i18n.locale = :ru
    config.i18n.default_locale = :ru
    config.encoding = "utf-8"
    config.action_dispatch.default_charset = "utf-8"

    config.filter_parameters += [:password]

    config.assets.enabled = true
    config.assets.version = '1.0'
    config.assets.paths << Rails.root.join("app", "assets", "flash")
    config.assets.paths << Rails.root.join("app", "assets", "fonts")

    config.assets.precompile += %w( snow.css w.css w.js swfobject.js hw.css chatroom.js )
    #config.assets.precompile += %w( tinymce/skins/charcoal/skin.min.css tinymce/skins/charcoal/content.min.css )

    config.app_generators do |g|
      g.template_engine :haml
      g.test_framework :rspec, :fixture => true, :views => false
      g.fixture_replacement :factory_girl, :dir => 'spec/factories'
    end
  end
end

Squeel.configure do |config|
  config.load_core_extensions :symbol
end

如果我添加需要factory_girl_rails ,为什么显示常量FacroryGirl的错误?我怎么解决这个问题?

谢谢你提前。

修改

rake test:prepare --trace

dima@dima-SATELLITE-C660:~/myapp/bdsmgalaxy$ rake test:prepare --trace
** Invoke test:prepare (first_time)
** Invoke db:test:prepare (first_time)
** Invoke db:abort_if_pending_migrations (first_time)
** Invoke environment (first_time)
** Execute environment

Sphinx cannot be found on your system. You may need to configure the following
settings in your config/sphinx.yml file:
  * bin_path
  * searchd_binary_name
  * indexer_binary_name


For more information, read the documentation:
http://pat.github.com/ts/en/advanced_config.html

rake aborted!
NameError: uninitialized constant FactoryGirl
/home/dima/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rspec-core-2.5.2/lib/rspec/core/backward_compatibility.rb:20:in `const_missing'
/home/dima/myapp/bdsmgalaxy/spec/factories/polls.rb:3:in `<top (required)>'
/home/dima/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/factory_girl-1.3.3/lib/factory_girl/factory.rb:314:in `block (2 levels) in find_definitions'
/home/dima/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/factory_girl-1.3.3/lib/factory_girl/factory.rb:313:in `each'
/home/dima/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/factory_girl-1.3.3/lib/factory_girl/factory.rb:313:in `block in find_definitions'
/home/dima/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/factory_girl-1.3.3/lib/factory_girl/factory.rb:308:in `each'
/home/dima/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/factory_girl-1.3.3/lib/factory_girl/factory.rb:308:in `find_definitions'
/home/dima/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/factory_girl_rails-1.0.1/lib/factory_girl_rails/railtie.rb:11:in `block in <class:Railtie>'
/home/dima/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/activesupport-3.2.22/lib/active_support/lazy_load_hooks.rb:34:in `call'
/home/dima/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/activesupport-3.2.22/lib/active_support/lazy_load_hooks.rb:34:in `execute_hook'
/home/dima/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/activesupport-3.2.22/lib/active_support/lazy_load_hooks.rb:43:in `block in run_load_hooks'
/home/dima/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/activesupport-3.2.22/lib/active_support/lazy_load_hooks.rb:42:in `each'
/home/dima/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/activesupport-3.2.22/lib/active_support/lazy_load_hooks.rb:42:in `run_load_hooks'
/home/dima/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/railties-3.2.22/lib/rails/application/finisher.rb:59:in `block in <module:Finisher>'
/home/dima/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/railties-3.2.22/lib/rails/initializable.rb:30:in `instance_exec'
/home/dima/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/railties-3.2.22/lib/rails/initializable.rb:30:in `run'
/home/dima/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/railties-3.2.22/lib/rails/initializable.rb:55:in `block in run_initializers'
/home/dima/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/railties-3.2.22/lib/rails/initializable.rb:54:in `each'
/home/dima/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/railties-3.2.22/lib/rails/initializable.rb:54:in `run_initializers'
/home/dima/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/railties-3.2.22/lib/rails/application.rb:136:in `initialize!'
/home/dima/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/railties-3.2.22/lib/rails/railtie/configurable.rb:30:in `method_missing'
/home/dima/myapp/bdsmgalaxy/config/environment.rb:5:in `<top (required)>'
/home/dima/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/railties-3.2.22/lib/rails/application.rb:103:in `require_environment!'
/home/dima/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/railties-3.2.22/lib/rails/application.rb:305:in `block (2 levels) in initialize_tasks'
/home/dima/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/task.rb:240:in `call'
/home/dima/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/task.rb:240:in `block in execute'
/home/dima/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/task.rb:235:in `each'
/home/dima/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/task.rb:235:in `execute'
/home/dima/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/task.rb:179:in `block in invoke_with_call_chain'
/home/dima/.rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'
/home/dima/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/task.rb:172:in `invoke_with_call_chain'
/home/dima/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/task.rb:201:in `block in invoke_prerequisites'
/home/dima/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/task.rb:199:in `each'
/home/dima/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/task.rb:199:in `invoke_prerequisites'
/home/dima/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/task.rb:178:in `block in invoke_with_call_chain'
/home/dima/.rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'
/home/dima/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/task.rb:172:in `invoke_with_call_chain'
/home/dima/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/task.rb:201:in `block in invoke_prerequisites'
/home/dima/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/task.rb:199:in `each'
/home/dima/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/task.rb:199:in `invoke_prerequisites'
/home/dima/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/task.rb:178:in `block in invoke_with_call_chain'
/home/dima/.rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'
/home/dima/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/task.rb:172:in `invoke_with_call_chain'
/home/dima/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/task.rb:201:in `block in invoke_prerequisites'
/home/dima/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/task.rb:199:in `each'
/home/dima/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/task.rb:199:in `invoke_prerequisites'
/home/dima/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/task.rb:178:in `block in invoke_with_call_chain'
/home/dima/.rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'
/home/dima/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/task.rb:172:in `invoke_with_call_chain'
/home/dima/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/task.rb:165:in `invoke'
/home/dima/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/application.rb:150:in `invoke_task'
/home/dima/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/application.rb:106:in `block (2 levels) in top_level'
/home/dima/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/application.rb:106:in `each'
/home/dima/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/application.rb:106:in `block in top_level'
/home/dima/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/application.rb:115:in `run_with_threads'
/home/dima/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/application.rb:100:in `top_level'
/home/dima/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/application.rb:78:in `block in run'
/home/dima/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/application.rb:176:in `standard_exception_handling'
/home/dima/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/application.rb:75:in `run'
/home/dima/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.4.2/bin/rake:33:in `<top (required)>'
/home/dima/.rbenv/versions/2.0.0-p247/bin/rake:23:in `load'
/home/dima/.rbenv/versions/2.0.0-p247/bin/rake:23:in `<main>'
Tasks: TOP => test:prepare => db:test:prepare => db:abort_if_pending_migrations => environment

当我从 FactoryGirl.define do ... 更改为 Factory.define do ... 时,显示错误

/home/dima/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/factory_girl-1.3.3/lib/factory_girl/factory.rb:54:在define': wrong number of arguments (0 for 1..2) (ArgumentError) from /home/dima/myapp/bdsmgalaxy/spec/factories/polls.rb:3:in'

1 个答案:

答案 0 :(得分:1)

我创立了解决方案。我将版本gem factory_girl-rails从1.1更新到3.0。旧版gem使用旧语法。