在 Rails 4.0.0.rc1、Ruby 2.0.0 上运行迁移后,当我尝试通过 rspec 运行测试时看到以下错误:
On Rails 4.0.0.rc1, Ruby 2.0.0, after I run a migration, I see the following error when I try to run a test through rspec:
/Users/peeja/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0.rc1/lib/active_record/migration.rb:376:in`check_pending!':迁移正在等待;运行 'rake db:migrateRAILS_ENV=test' 来解决这个问题.(ActiveRecord::PendingMigrationError)
/Users/peeja/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0.rc1/lib/active_record/migration.rb:376:in `check_pending!': Migrations are pending; run 'rake db:migrate RAILS_ENV=test' to resolve this issue. (ActiveRecord::PendingMigrationError)
这似乎不对.没有人迁移他们的测试数据库,是吗?他们db:test:prepare 它,公平地说,我忘了这样做.所以我运行 rake db:test:prepare 并再次运行我的 rspec 命令......并看到相同的错误.
That doesn't seem right. No one migrates their test database, do they? They db:test:prepare it, which—to be fair—I've forgotten to do. So I run rake db:test:prepare and run my rspec command again…and see the same error.
如果我真的rake db:migrate RAILS_ENV=test,错误确实会消失.
If I actually rake db:migrate RAILS_ENV=test, the error does in fact go away.
这是怎么回事?这是 Rails 4 的新功能吗?
What's going on? Is this new in Rails 4?
推荐答案从 Rails 4.1 开始,不推荐使用 rake db:test:* 任务.相反,您的 (test|spec)_helper.rb 应该包括:
As of Rails 4.1, the rake db:test:* tasks are deprecated. Instead, your (test|spec)_helper.rb should include:
ActiveRecord::Migration.maintain_test_schema!这意味着每次运行测试时,您的测试数据库都会获得正确的架构,无论您是否从 Rake 任务中运行它们.
This means that your test database will get the correct schema every time your tests run, whether you run them from a Rake task or not.
更多推荐
为什么要求我运行“rake db:migrate RAILS
发布评论