本文介绍了无法使factory_girl在rails 3.0.5下运行,意外的tCONSTANT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的Gemfile配置:

This is my Gemfile config:

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,因为我想将它们保存在单独的文件夹中.

I added the factories folder to the LOAD_PATH, because I want to keep them in a separate folder.

这是我的factories.rb文件:

需要File.expand_path(File.dirname( FILE )+'../../spec_helper')

require File.expand_path(File.dirname(FILE) + '../../spec_helper')

Factory.define(:user) do |f|
  f.country("China")
  ... other attributes here
end

当我使用rake spec:models运行测试时,我得到了:

When I run the tests, using rake spec:models, I get this:

spec/factories/factories.rb:1: syntax error, unexpected tCONSTANT, expecting $end

我看到这源自factory_girlfind_definitions方法.我尝试通过spec_helper自己调用它,但是它没有任何改变.这是堆栈跟踪的一部分:

I see that this originates from factory_girl's find_definitions method. I tried calling this myself, from the spec_helper, but it doesn't change anything. Here's part of the stack trace:

** 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文件

I think problem is related to the loading of your factory.Just write this in your test_helper.rb file

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

   OR

   require 'factory_girl'
   FactoryGirl.find_definitions

这篇关于无法使factory_girl在rails 3.0.5下运行,意外的tCONSTANT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 08:25