How do I prepare test database(s) for Rails rspec tests without running rake spec?

后端 未结 6 1779
無奈伤痛
無奈伤痛 2020-12-04 14:28

After significant troubleshooting, I figured out that I needed to run rake spec once (I can abort with control-c) before I can run rspec directly (e.g. on a sub

6条回答
  •  情歌与酒
    2020-12-04 14:42

    In a spring-ified Rails 4 app, my bin/setup is usually augmented to contain

    puts "\n== Preparing test database =="
    system "RAILS_ENV=test bin/rake db:setup"
    

    This is very similar to leviathan's answer, plus seeding the test DB, as

    rake db:setup # Create the database, load the schema, and initialize with the seed data
    (use
    db:reset to also drop the database first)

    As the comment mentions, if we want to drop the DB first, rake db:reset does just that.

    I also find that this provides more feedback when compared to rake db:test:prepare.

提交回复
热议问题