为什么要求我运行“rake db:migrate RAILS_ENV=test"? [英] Why am I asked to run 'rake db:migrate RAILS_ENV=test'?

查看:67
本文介绍了为什么要求我运行“rake db:migrate RAILS_ENV=test"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 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_ENV=test"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆