railstutorial.org - undefined method `Factory'

前端 未结 10 1433
时光取名叫无心
时光取名叫无心 2021-02-14 08:06

I\'m attempting to follow railstutorial.org, and am currently on Chapter 7, where you start using factories: http://railstutorial.org/chapters/modeling-and-viewing-users-two#sec

相关标签:
10条回答
  • 2021-02-14 08:22

    I was determined to use the newest version of Factory Girl, so I tried to adapt the code. Didn't work for me, so I used

    gem 'factory_girl_rails', '1.0'
    

    in the Gemfile to lock the version at 1.0

    bundle update
    

    restart spork and autotest and it worked.

    0 讨论(0)
  • 2021-02-14 08:23

    I had this problem, but it was because I had placed the factory girl gem under the development section instead of the test section of the Gemfile. Once under the test section, it worked. One difference I note between my entry and yours is that mine specifies 1.0:

    group :test do
      gem 'rspec-rails', '2.6.1'
      gem 'webrat', '0.7.1'
      gem 'factory_girl_rails', '1.0'
    end
    
    0 讨论(0)
  • 2021-02-14 08:24

    My solution: I've accidentally included it in the :development block, and simply had to move it to the :test block

    (I've listed it here, because it might help someone who doesn't follow the tutorial correctly)

    0 讨论(0)
  • 2021-02-14 08:25

    I got this exact same error message. I just restarted my Spork server and Autotest and everything went green for me.

    0 讨论(0)
  • 2021-02-14 08:35

    Maybe you should try the new syntax (see github readme of factory girl)

    FactoryGirl.define :user do |user|
      user.name                  "Michael Hartl"
      user.email                 "mhartl@example.com"
      user.password              "foobar"
      user.password_confirmation "foobar"
    end
    
    0 讨论(0)
  • 2021-02-14 08:37

    For those finding this page now: note where you once used "FactoryGirl" you must now use "FactoryBot" in your tests. From the thoughtbot announcement page:

    "We’re renaming factory_girl to factory_bot (and factory_girl_rails to factory_bot_rails). All the same functionality of factory_girl, now under a different name."

    More details here:

    https://robots.thoughtbot.com/factory_bot

    0 讨论(0)
提交回复
热议问题