Cannot get factory_girl running under rails 3.0.5,unexpected tCONSTANT

后端 未结 2 1086
旧时难觅i
旧时难觅i 2021-01-04 13:13

This is my Gemfile config:

group :development, :test do
    gem \'rspec-rails\'
    gem \'factory_girl\', \'~>2.0.0.beta1\'
    gem \'factory_girl_rails\'         


        
相关标签:
2条回答
  • 2021-01-04 13:54

    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.

    0 讨论(0)
  • 2021-01-04 14:06

    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
    
    0 讨论(0)
提交回复
热议问题