【发布时间】:2016-10-28 16:57:36
【问题描述】:
我正在构建一个 Rails 5 应用程序,并且正在使用 Rspec 进行测试。我过去曾多次将 Travis 用于 Rails 应用程序,但他们使用的是 MiniTest 和 Rails 4。
我可以在本地运行测试:
joenyland@MacBook-Pro ~/Code/acrm $ rake db:drop
Dropped database 'db/development.sqlite3'
Database 'db/test.sqlite3' does not exist
joenyland@MacBook-Pro ~/Code/acrm $ RAILS_ENV=test bundle exec rake
/Users/joenyland/.rvm/rubies/ruby-2.3.0/bin/ruby -I/Users/joenyland/.rvm/gems/ruby-2.3.0@acrm/gems/rspec-core-3.5.4/lib:/Users/joenyland/.rvm/gems/ruby-2.3.0@acrm/gems/rspec-support-3.5.0/lib /Users/joenyland/.rvm/gems/ruby-2.3.0@acrm/gems/rspec-core-3.5.4/exe/rspec --pattern spec/\*\*\{,/\*/\*\*\}/\*_spec.rb
*.............................
Pending: (Failures listed here are expected and do not affect your suite's status)
1) SessionsHelper add some examples to (or delete) /Users/joenyland/Code/acrm/spec/helpers/sessions_helper_spec.rb
# Not yet implemented
# ./spec/helpers/sessions_helper_spec.rb:14
Finished in 1.87 seconds (files took 15.63 seconds to load)
30 examples, 0 failures, 1 pending
但是当 Travis 运行测试时,它失败了:
0K$ bundle exec rake
/home/travis/.rvm/rubies/ruby-2.3.0/bin/ruby -I/home/travis/build/MasterRoot24/acrm/vendor/bundle/ruby/2.3.0/gems/rspec-core-3.5.4/lib:/home/travis/build/MasterRoot24/acrm/vendor/bundle/ruby/2.3.0/gems/rspec-support-3.5.0/lib /home/travis/build/MasterRoot24/acrm/vendor/bundle/ruby/2.3.0/gems/rspec-core-3.5.4/exe/rspec --pattern spec/\*\*\{,/\*/\*\*\}/\*_spec.rb
/home/travis/build/MasterRoot24/acrm/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0.1/lib/active_record/migration.rb:572:in `check_pending!': (ActiveRecord::PendingMigrationError)
Migrations are pending. To resolve this issue, run:
bin/rails db:migrate RAILS_ENV=test
from /home/travis/build/MasterRoot24/acrm/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0.1/lib/active_record/migration.rb:585:in `load_schema_if_pending!'
from /home/travis/build/MasterRoot24/acrm/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0.1/lib/active_record/migration.rb:591:in `block in maintain_test_schema!'
from /home/travis/build/MasterRoot24/acrm/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0.1/lib/active_record/migration.rb:822:in `suppress_messages'
from /home/travis/build/MasterRoot24/acrm/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0.1/lib/active_record/migration.rb:596:in `method_missing'
from /home/travis/build/MasterRoot24/acrm/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0.1/lib/active_record/migration.rb:591:in `maintain_test_schema!'
from /home/travis/build/MasterRoot24/acrm/spec/rails_helper.rb:27:in `<top (required)>'
from /home/travis/build/MasterRoot24/acrm/spec/models/client_spec.rb:1:in `require'
from /home/travis/build/MasterRoot24/acrm/spec/models/client_spec.rb:1:in `<top (required)>'
from /home/travis/build/MasterRoot24/acrm/vendor/bundle/ruby/2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/configuration.rb:1435:in `load'
from /home/travis/build/MasterRoot24/acrm/vendor/bundle/ruby/2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/configuration.rb:1435:in `block in load_spec_files'
from /home/travis/build/MasterRoot24/acrm/vendor/bundle/ruby/2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/configuration.rb:1433:in `each'
from /home/travis/build/MasterRoot24/acrm/vendor/bundle/ruby/2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/configuration.rb:1433:in `load_spec_files'
from /home/travis/build/MasterRoot24/acrm/vendor/bundle/ruby/2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/runner.rb:100:in `setup'
from /home/travis/build/MasterRoot24/acrm/vendor/bundle/ruby/2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/runner.rb:86:in `run'
from /home/travis/build/MasterRoot24/acrm/vendor/bundle/ruby/2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/runner.rb:71:in `run'
from /home/travis/build/MasterRoot24/acrm/vendor/bundle/ruby/2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/runner.rb:45:in `invoke'
from /home/travis/build/MasterRoot24/acrm/vendor/bundle/ruby/2.3.0/gems/rspec-core-3.5.4/exe/rspec:4:in `<main>'
/home/travis/.rvm/rubies/ruby-2.3.0/bin/ruby -I/home/travis/build/MasterRoot24/acrm/vendor/bundle/ruby/2.3.0/gems/rspec-core-3.5.4/lib:/home/travis/build/MasterRoot24/acrm/vendor/bundle/ruby/2.3.0/gems/rspec-support-3.5.0/lib /home/travis/build/MasterRoot24/acrm/vendor/bundle/ruby/2.3.0/gems/rspec-core-3.5.4/exe/rspec --pattern spec/\*\*\{,/\*/\*\*\}/\*_spec.rb failed
我的 Travis 配置:
language: ruby
cache: bundler
我知道显而易见的解决方案是在 Travis 配置中添加一个 before 脚本以运行 rails db:migrate RAILS_ENV=test。但是,我的问题是,为什么在我以前从未需要这样做的情况下突然需要这样做?
Rails 5 的 Rspec 实现是否发生了一些变化,这意味着迁移不会在测试之前自动运行? (我假设这就是过去发生的事情......)
我应该补充一点,spec/rails_helper.rb 包含以下行:
# Checks for pending migration and applies them before tests are run.
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.maintain_test_schema!
...所以看起来它应该正在运行迁移。
2016 年 3 月 11 日更新
在 Travis 的帮助下,我设法在 Travis 的基础架构之外重现了这个问题。
Travis 在其基于容器的基础架构上运行构建,其映像 (quay.io/travisci/travis-ruby) 基于 Ubuntu 12.04.5 (Precise)。
我现在可以像这样重新创建问题:
docker run -it ubuntu:precise /bin/bash
apt-get update && apt-get install git bash-completion curl nodejs -y && gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 && \curl -sSL https://get.rvm.io | bash -s stable && source /etc/profile.d/rvm.sh && rvm install 2.3.0 && git clone --depth=50 --branch=master https://github.com/MasterRoot24/acrm.git MasterRoot24/acrm && cd MasterRoot24/acrm && rvm use 2.3.0 && gem install bundler && export BUNDLE_GEMFILE=$PWD/Gemfile && bundle install --jobs=3 --retry=3 --deployment --path=${BUNDLE_PATH:-vendor/bundle} && bundle exec rake
root@e4f741e8c189:/MasterRoot24/acrm# bundle exec rake
/usr/local/rvm/rubies/ruby-2.3.0/bin/ruby -I/MasterRoot24/acrm/vendor/bundle/ruby/2.3.0/gems/rspec-core-3.5.4/lib:/MasterRoot24/acrm/vendor/bundle/ruby/2.3.0/gems/rspec-support-3.5.0/lib /MasterRoot24/acrm/vendor/bundle/ruby/2.3.0/gems/rspec-core-3.5.4/exe/rspec --pattern spec/\*\*\{,/\*/\*\*\}/\*_spec.rb
/MasterRoot24/acrm/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0.1/lib/active_record/migration.rb:572:in `check_pending!': (ActiveRecord::PendingMigrationError)
Migrations are pending. To resolve this issue, run:
bin/rails db:migrate RAILS_ENV=test
from /MasterRoot24/acrm/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0.1/lib/active_record/migration.rb:585:in `load_schema_if_pending!'
from /MasterRoot24/acrm/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0.1/lib/active_record/migration.rb:591:in `block in maintain_test_schema!'
如果我在 macOS 10.12 或 Debian Jessie 本地执行相同的操作,则测试通过,并且不会要求我运行手动迁移。
为什么我需要在 Ubuntu 12.04 下运行手动迁移,而不是在任何其他操作系统下运行?
【问题讨论】:
标签: ruby-on-rails rspec travis-ci rspec-rails ruby-on-rails-5