use*_*003 6 rspec ruby-on-rails rails-models rails-migrations rails-engines
我正在开发一个 Rails 引擎(插件)。到目前为止我有
MyJobspec/models/my_job_spec.rbrails 模板会自动创建一个spec/dummy应用程序,因此我执行以下操作来运行我的测试
# Create the development and test DBs
rake db:create
# Copy migrations over to my dummy app
cd spec/dummy
rake my_app:install:migrations
cd ../..
# Run specs
rspec spec/models/my_job_spec.rb
Run Code Online (Sandbox Code Playgroud)
但是,当我运行规范时,出现错误:
> rspec spec/models/
/Users/jeeves/.rvm/gems/ruby-2.2.2@gb/gems/activerecord-5.1.0/lib/active_record/migration.rb:576:in `check_pending!': (ActiveRecord::PendingMigrationError)
Migrations are pending. To resolve this issue, run:
bin/rails db:migrate RAILS_ENV=test
Run Code Online (Sandbox Code Playgroud)
我认为这会自动发生,因为rails_helper.rb我肯定有以下内容,这应该为我维护我的测试模式
ActiveRecord::Migration.maintain_test_schema!
Run Code Online (Sandbox Code Playgroud)
它与插件或引擎的工作方式不同吗?
编辑:我尝试了运行bin/rails db:migrate RAILS_ENV=test, insidespec/dummy/然后重新运行的建议rspec spec/。仍然没有运气。
好吧,对于其他偶然发现这个问题的人来说,我发现了一些我做错的事情。
首先,我不需要rake my_app:installations:migrations在测试或开发时运行。显然,这仅适用于我的应用程序的下游用户,当他们想要将我的引擎的迁移复制到他们的主机应用程序中时。回想起来,各种博客等中的说明都提到了这一点,但我仍然认为,如果这是您第一次尝试弄清楚它,那么很容易误解。
其次,所有命令都只能从引擎的根目录执行,rails 确保它们被正确应用。因此,只需rake db:migrate从引擎根目录运行即可,虚拟应用程序将被迁移。
第三,我停止使用ActiveRecord::Migration.maintain_test_schema!. 正如上面的用户所指出的,它并没有真正像广告中那样工作。rake db:migrate RAILS_ENV=test正确迁移了我的测试数据库,但由于嵌套虚拟应用程序的设置方式,该maintain_test_schema!方法没有迁移任何内容。不能在两个环境中自动迁移它有点烦人,但我会接受。
所以整个过程最终非常简单,可以归结为:
# Create the development and test DBs
rake db:create
# Migrate
rake db:migrate
rake db:migrate RAILS_ENV=test
# Run specs
rspec spec/models/my_job_spec.rb
Run Code Online (Sandbox Code Playgroud)
回想起来,这似乎很简单,但在过程中学到了一些东西。