FactoryGirl screws up rake db:migrate process

后端 未结 4 1456
梦毁少年i
梦毁少年i 2020-12-04 18:00

I am doing TDD/BDD in Ruby on Rails 3 with Rspec (2.11.0) and FactoryGirl (4.0.0). I have a factory for a Category model:

FactoryGirl.define \"Category\" do         


        
4条回答
  •  猫巷女王i
    2020-12-04 18:39

    A simple fix to this issue is to delay evaluation of any models in your factories by wrapping them in blocks. So, instead of this:

    factory :cake do
      name "Delicious Cake"
      frosting Frosting.new(:flavor => 'chocolate')
      filling Filling.new(:flavor => 'red velvet')
    end
    

    Do this (notice the curly braces):

    factory :cake do
      name "Delicious Cake in a box"
      frosting { Frosting.new(:flavor => 'chocolate') }
      filling { Filling.new(:flavor => 'red velvet') }
    end
    

    If you have a lot of factories this may not be feasible, but it is rather straightforward. See also here.

提交回复
热议问题