Cannot get factory_girl running under rails 3.0.5,unexpected tCONSTANT

心不动则不痛 提交于 2019-11-30 18:35:08
Ashish

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!