Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot get factory_girl running under rails 3.0.5,unexpected tCONSTANT

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

This is my 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}

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

This is my factories.rb file:

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

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

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

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

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
like image 296
Geo Avatar asked Mar 14 '11 12:03

Geo


2 Answers

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
like image 66
Ashish Avatar answered Nov 16 '22 03:11

Ashish


I had the same issue, but it turns out that I simply named my file wrong (factories.rb.rb, thanks to Netbeans). I discovered a few things that would be useful to this page, though.

  1. From the getting started page. Factory girl should automatically load the following files if they exist:

    test/factories.rb
    spec/factories.rb
    test/factories/*.rb
    spec/factories/*.rb
    

    Putting the following in test_helper.rb can cause double loading:

    require 'factory_girl' Factory.find_definitions

    This caused me to get a `add_as': Already defined: <factory name> error.

  2. The factory_girl_rails gem automatically loads factory_girl. It's unnecessary, but doesn't appear to have any side effects.

  3. I discovered that the FactoryGirl syntax has changed quite a bit since I first learned it in March 2011. I highly recommend everyone check out the getting started page to see some of the changes.

like image 25
Eric Hu Avatar answered Nov 16 '22 03:11

Eric Hu