I have some seed data (for price ranges) that is the same in prod, dev, test and doesn’t change. I need that data in my test db to run my cuke tests.
I am load my seed data into test DB before the scenario loads, but it’s failing.
I have the following in my features/support/env.rb file
# from http://www.andhapp.com/blog/2009/11/07/using-factory_girl-with-cucumber/
Before do
require 'factory_girl_rails'
# Dir.glob(File.join(File.dirname(__FILE__), '../../spec/factories/*.rb')).each {|f| require f }
Dir.glob(File.join(File.dirname(__FILE__), '../../db/seeds.rb')).each {|f| require f }
end
Which loads the following file:
# wipe out all previous data
Price.delete_all #is there a factory way of doing this?
# set defaults
Factory.define :price do |price|
price.id 1
price.price_range "$100"
end
# insert seed data
@price = Factory(:price, :id => 1, :price_range => "$100 - $500")
@price = Factory(:price, :id => 2, :price_range => "$500 - $1,000")
@price = Factory(:price, :id => 3, :price_range => "$1,000 - $1,000")
@price = Factory(:price, :id => 4, :price_range => "$10,000 - $100,000")
I get the following error message:
Factory already defined: price (Factory::DuplicateDefinitionError)
/Library/Ruby/Gems/1.8/gems/factory_girl-1.3.3/lib/factory_girl/factory.rb:61:in `define'
/Applications/MAMP/htdocs/rails_testing/feedbackd/features/support/../../db/seeds.rb:16
/Library/Ruby/Gems/1.8/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:239:in `require'
/Library/Ruby/Gems/1.8/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:239:in `require'
/Library/Ruby/Gems/1.8/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:227:in `load_dependency'
/Library/Ruby/Gems/1.8/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:239:in `require'
/Applications/MAMP/htdocs/rails_testing/feedbackd/features/support/env.rb:92
/Applications/MAMP/htdocs/rails_testing/feedbackd/features/support/env.rb:92:in `each'
/Applications/MAMP/htdocs/rails_testing/feedbackd/features/support/env.rb:92:in `Before'
Any thoughts?
You can only call
Factory.define :priceonce, and I would probably not put it in the file that it’s in right now. Do you have a factories folder? It usually lives in spec/factories. In there I would create the file price.rb, and define your factory once, there. factory_girl should automatically load all of those definitions up for you once.If you’re using Rails3 and you have factory_girl_rails in your Gemfile, then you don’t even need that
require 'factory_girl_rails', it does it for you.Also if you have a new-ish version of cucumber the installer should automatically have added this section for you in env.rb:
DatabaseCleaner is a good way to do the truncation instead of using your
Price.delete_allThe last thing is seeds.rb – It’s a good concept and something very similar to what we do in one of our apps at work.
But keep in mind, everything in the
features/supportdirectory is automatically required by cucumber, so you don’t need to have that Dir.glob nonsense.With factory_girl we don’t use the default rails seeds file because it’s not really applicable (in our opinion).
I would just add a file named anything (ours is named
db_setup.rb) that looks something like this: