Ruby on rails 不';t在测试中创建迁移(I';m使用rspec),但在开发中它可以工作

Ruby on rails 不';t在测试中创建迁移(I';m使用rspec),但在开发中它可以工作,ruby-on-rails,Ruby On Rails,这是我的控制台: ilya@SamsungRV-509:~/MyProjects/easy_learning$ rake db:migrate RAILS_ENV=test == SetPasswordToAdministrator: migrating ===================================== DEPRECATION WARNING: This dynamic method is deprecated. Please use e.g. Post.find_o

这是我的控制台:

ilya@SamsungRV-509:~/MyProjects/easy_learning$ rake db:migrate RAILS_ENV=test
==  SetPasswordToAdministrator: migrating =====================================
DEPRECATION WARNING: This dynamic method is deprecated. Please use e.g. Post.find_or_create_by(name: 'foo') instead. (called from up at /home/ilya/MyProjects/easy_learning/db/migrate/20131210185519_set_password_to_administrator.rb:3)
[deprecated] I18n.enforce_available_locales will default to true in the future. If you really want to skip validation of your locale you can set I18n.enforce_available_locales = false to avoid this message.
rake aborted!
An error has occurred, all later migrations canceled:

undefined method `mobile' for #<User:0xba972894>/home/ilya/MyProjects/easy_learning/db/migrate/20131210185519_set_password_to_administrator.rb:3:in `up'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
ilya@SamsungRV-509:~/MyProjects/easy\u learning$rake db:migrate RAILS\u ENV=test
==将密码设置为管理员:正在迁移=====================================
弃用警告:此动态方法已弃用。请使用Post.find_或create_by(名称:'foo')代替。(从/home/ilya/MyProjects/easy\u learning/db/migrate/20131210185519\u设置密码\u至\u管理员。rb:3)
[已弃用]I18n.enforce\u available\u区域设置将在将来默认为true。如果确实要跳过区域设置的验证,可以将I18n.enforce\u available\u locales=false设置为避免出现此消息。
雷克流产了!
发生错误,所有后续迁移均已取消:
#/home/ilya/MyProjects/easy_learning/db/migrate/20131210185519的未定义方法“mobile”设置为“管理员”。rb:3:in“up”
任务:TOP=>db:migrate
(通过使用--trace运行任务查看完整跟踪)
这是我的档案:

class SetPasswordToAdministrator < ActiveRecord::Migration
  def up
    admin = ::User.find_or_create_by_email_and_name("admin@email.com", "Admin")
    admin.password = admin.password_confirmation = "easylearning"
    admin.role = "Administrator"
    admin.save!
  end
end
class SetPasswordToAdministrator
您需要先运行两个rake任务(或确保它们已运行),才能开始Rspec,因为它与您的数据库相关:

rake db:create:all
这将确保您的测试数据库已创建

rake db:migrate db:test:clone
这将确保您的测试数据库是最新的,并且可以运行您的规范


在过去的一年里,由于我一直在努力改进T/BDD和Rspec,我甚至不再单独运行
rake db:migrate
。我总是现在就运行
rake db:migrate db:test:clone

如果您运行的是Rails 4.1+,RSpec有一个功能,可以使您的测试数据库与开发保持同步。这可从RSpec网站获得:

要利用此功能,请在需要rails之后,将以下内容添加到rails\u帮助文件的顶部:

ActiveRecord::Migration.maintenant\u test\u schema

Rails 4.0中的行为略有不同。以下是有关详细信息的页面: