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

后端 未结 6 1783
無奈伤痛
無奈伤痛 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条回答
  •  猫巷女王i
    2020-12-04 14:46

    I would recommend dropping your test database, then re-create it and migrate:

    bundle exec rake db:drop RAILS_ENV=test
    bundle exec rake db:create RAILS_ENV=test
    bundle exec rake db:schema:load RAILS_ENV=test
    

    After these steps you can run your specs:

    bundle exec rspec spec
    

    gerry3 noted that:

    A simpler solution is to just run rake db:test:prepare

    However, if you're using PostgreSQL this wont work because the rails environment gets loaded, which opens a database connection. This causes the prepare call to fail, because the DB cannot be dropped. Tricky thing.

提交回复
热议问题