Cannot get factory_girl running under rails 3.0.5,unexpected tCONSTANT
Asked Answered
T

2

10

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
Tocopherol answered 14/3, 2011 at 12:11 Comment(5)
Comment the first line (require 'spec_helper' ) and try it.Swift
There is some progress, the test starts running, but at the line saying: u = Factory(:user).create it throws a ArgumentError, Not registered: user, exception.Tocopherol
u = Factory(:user) line only create user for youSwift
I get the same exception if I remove the .create from the line.Tocopherol
Just delete the factory folder and keep your factory file in spec folder and test it. If it is working then your problem is related to loading.Swift
S
26

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
Swift answered 14/3, 2011 at 12:50 Comment(3)
Yeah, it was loading related. Moving the factory file in the spec dir solved the problem.Tocopherol
btw, change Factory.find_definitions to FactoryGirl.find_definitions Factory has been depreceatedNewspaperwoman
Just did it and I got a message to change Factory.find_definitions to FactoryGirl.find_definitions.Nevanevada
D
6

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.

Decasyllabic answered 12/10, 2011 at 20:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.