Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

With RSpec, how to seed the database on load?

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

like image 434
AnApprentice Avatar asked May 14 '11 18:05

AnApprentice


People also ask

What is RSpec seed?

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).

What is DB seed in rails?

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.

What is a seed file database?

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.

Does RSpec clean database?

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.


1 Answers

In spec_helper.rb or rails_helper.rb:

RSpec.configure do |config|   config.before(:suite) do     Rails.application.load_seed # loading seeds   end end 
like image 104
Hannes Avatar answered Sep 20 '22 16:09

Hannes