gpt4 book ai didi

ruby-on-rails - 无法让 factory_girl 在 rails 3.0.5 下运行,意外的 tCONSTANT

转载 作者:行者123 更新时间:2023-12-04 02:38:13 24 4
gpt4 key购买 nike

这是我的 Gemfile 配置:

group :development, :test do
gem 'rspec-rails'
gem 'factory_girl', '~>2.0.0.beta1'
gem 'factory_girl_rails', :git => 'https://github.com/thoughtbot/factory_girl_rails.git', :tag => 'v1.1.beta1'
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 "factory_girl"

# 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}
Dir[Rails.root.join("spec/factories/**/*.rb")].each {|f| require f}

我添加了 factories文件夹到 LOAD_PATH,因为我想将它们保存在一个单独的文件夹中。

这是我的 factories.rb文件:

需要 File.expand_path(File.dirname( FILE ) + '../../spec_helper')
Factory.define(:user) do |f|
f.country("China")
... other attributes here
end

当我运行测试时,使用 rake spec:models ,我明白了:
spec/factories/factories.rb:1: syntax error, unexpected tCONSTANT, expecting $end

我看到这源自 factory_girlfind_definitions方法。我尝试从 spec_helper 自己调用它,但它没有改变任何东西。这是堆栈跟踪的一部分:
** Invoke spec:models (first_time)
** Invoke db:test:prepare (first_time)
** Invoke db:abort_if_pending_migrations (first_time)
** Invoke environment (first_time)
** Execute environment
rake aborted!

C:/rails/rcproj/spec/factories/factories.rb:1: syntax error, unexpected tCONSTANT, expecting
$end
f.count...er) do |f|
^
C:/Ruby187/lib/ruby/gems/1.8/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:239:in `req
uire'
C:/Ruby187/lib/ruby/gems/1.8/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:239:in `req
uire'
C:/Ruby187/lib/ruby/gems/1.8/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:225:in `loa
d_dependency'
C:/Ruby187/lib/ruby/gems/1.8/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:596:in `new
_constants_in'
C:/Ruby187/lib/ruby/gems/1.8/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:225:in `loa
d_dependency'
C:/Ruby187/lib/ruby/gems/1.8/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:239:in `req
uire'
C:/Ruby187/lib/ruby/gems/1.8/gems/factory_girl-2.0.0.beta2/lib/factory_girl/find_definitions.rb:20:i
n `find_definitions'
C:/Ruby187/lib/ruby/gems/1.8/gems/factory_girl-2.0.0.beta2/lib/factory_girl/find_definitions.rb:19:i
n `each'
C:/Ruby187/lib/ruby/gems/1.8/gems/factory_girl-2.0.0.beta2/lib/factory_girl/find_definitions.rb:19:i

最佳答案

我认为问题与您工厂的装载有关。
只需将其写入您的 test_helper.rb 文件中

   require 'factory_girl'
Dir.glob(File.dirname(__FILE__) + "/factories/*").each do |factory|
require factory
end

OR

require 'factory_girl'
FactoryGirl.find_definitions

关于ruby-on-rails - 无法让 factory_girl 在 rails 3.0.5 下运行,意外的 tCONSTANT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5298392/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com