I'm using rspec for testing w my rails 3 app. I need to seed the database before the tests start. How can I seed the database with the following:
/db/seeds.rb
["Admin", "Member"].each do |role_name| Role.find_or_create_by_name(role_name) end
Thanks
Seed for random ordering (default: generated randomly each run). When you run specs with --order random , RSpec generates a random seed for the randomization and prints it to the output_stream (assuming you're using RSpec's built-in formatters).
Rails seed files are a useful way of populating a database with the initial data needed for a Rails project. The Rails db/seeds. rb file contains plain Ruby code and can be run with the Rails-default rails db:seed task.
Database seeding is populating a database with an initial set of data. It's common to load seed data such as initial user accounts or dummy data upon initial setup of an application.
I use the database_cleaner gem to scrub my test database before each test runs, ensuring a clean slate and stable baseline every time. By default, RSpec will actually do this for you, running every test with a database transaction and then rolling back that transaction after it finishes.
In spec_helper.rb or rails_helper.rb:
RSpec.configure do |config| config.before(:suite) do Rails.application.load_seed # loading seeds end end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With