使用RAILS_ENV = test调用rake任务时的LoadError

时间:2014-04-21 11:46:55

标签: ruby-on-rails ruby rake rake-task

我在Spring使用Ruby 2.1.1和rails-4.1.0。

我无法使用RAILS_ENV = test属性启动rake。由于某些原因,rake命令

RAILS_ENV=test rake db:migrate

导致以下错误:

/home/user/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.0/lib/active_support/dependencies.rb:241:in `load': cannot load such file -- /home/user/rbpr/getLead/db:migrate (LoadError)
    from /home/user/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.0/lib/active_support/dependencies.rb:241:in `block in load'
    from /home/user/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.0/lib/active_support/dependencies.rb:232:in `load_dependency'
    from /home/user/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.0/lib/active_support/dependencies.rb:241:in `load'
    from /home/user/.rvm/gems/ruby-2.1.1/gems/rspec-core-2.14.8/lib/rspec/core/configuration.rb:896:in `block in load_spec_files'
    from /home/user/.rvm/gems/ruby-2.1.1/gems/rspec-core-2.14.8/lib/rspec/core/configuration.rb:896:in `each'
    from /home/user/.rvm/gems/ruby-2.1.1/gems/rspec-core-2.14.8/lib/rspec/core/configuration.rb:896:in `load_spec_files'
    from /home/user/.rvm/gems/ruby-2.1.1/gems/rspec-core-2.14.8/lib/rspec/core/command_line.rb:22:in `run'
    from /home/user/.rvm/gems/ruby-2.1.1/gems/rspec-core-2.14.8/lib/rspec/core/runner.rb:80:in `run'
    from /home/user/.rvm/gems/ruby-2.1.1/gems/rspec-core-2.14.8/lib/rspec/core/runner.rb:17:in `block in autorun'

我的Rakfefile未更改,默认为:

require File.expand_path('../config/application', __FILE__)
GetLead::Application.load_tasks

使用RAILS_ENV = development和RAILS_ENV = production我没有LoadErrors。 我怎么能抓住这个错误?

更新

我已经安装了测试数据库,并且没有待处理的迁移(所有迁移都处于RAILS_ENV = test状态。)

test:
  adapter: postgresql
  encoding: unicode
  database: pgdb_getlead_test
  pool: 15
  username: ***
  password: ***

我已更新RSpec,但仍有相同的错误:

/home/user/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.0/lib/active_support/dependencies.rb:241:in `load': cannot load such file -- /home/user/rbpr/getLead/db:reset (LoadError)
    from /home/user/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.0/lib/active_support/dependencies.rb:241:in `block in load'
    from /home/user/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.0/lib/active_support/dependencies.rb:232:in `load_dependency'
    from /home/user/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.0/lib/active_support/dependencies.rb:241:in `load'
    from /home/user/.rvm/gems/ruby-2.1.1/gems/rspec-core-3.0.0.beta1/lib/rspec/core/configuration.rb:886:in `block in load_spec_files'
    from /home/user/.rvm/gems/ruby-2.1.1/gems/rspec-core-3.0.0.beta1/lib/rspec/core/configuration.rb:886:in `each'
    from /home/user/.rvm/gems/ruby-2.1.1/gems/rspec-core-3.0.0.beta1/lib/rspec/core/configuration.rb:886:in `load_spec_files'
    from /home/user/.rvm/gems/ruby-2.1.1/gems/rspec-core-3.0.0.beta1/lib/rspec/core/command_line.rb:22:in `run'
    from /home/user/.rvm/gems/ruby-2.1.1/gems/rspec-core-3.0.0.beta1/lib/rspec/core/runner.rb:90:in `run'
    from /home/user/.rvm/gems/ruby-2.1.1/gems/rspec-core-3.0.0.beta1/lib/rspec/core/runner.rb:17:in `block in autorun'

rake db:test:prepare相同:

WARNING: db:test:prepare is deprecated. The Rails test helper now maintains your test schema automatically, see the release notes for details.
/home/user/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.0/lib/active_support/dependencies.rb:241:in `load': cannot load such file -- /home/user/rbpr/getLead/db:test:prepare (LoadError)
  from /home/user/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.0/lib/active_support/dependencies.rb:241:in `block in load'
  from /home/user/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.0/lib/active_support/dependencies.rb:232:in `load_dependency'
  from /home/user/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.0/lib/active_support/dependencies.rb:241:in `load'
  from /home/user/.rvm/gems/ruby-2.1.1/gems/rspec-core-3.0.0.beta1/lib/rspec/core/configuration.rb:886:in `block in load_spec_files'
  from /home/user/.rvm/gems/ruby-2.1.1/gems/rspec-core-3.0.0.beta1/lib/rspec/core/configuration.rb:886:in `each'
  from /home/user/.rvm/gems/ruby-2.1.1/gems/rspec-core-3.0.0.beta1/lib/rspec/core/configuration.rb:886:in `load_spec_files'
  from /home/user/.rvm/gems/ruby-2.1.1/gems/rspec-core-3.0.0.beta1/lib/rspec/core/command_line.rb:22:in `run'
  from /home/user/.rvm/gems/ruby-2.1.1/gems/rspec-core-3.0.0.beta1/lib/rspec/core/runner.rb:90:in `run'
  from /home/user/.rvm/gems/ruby-2.1.1/gems/rspec-core-3.0.0.beta1/lib/rspec/core/runner.rb:17:in `block in autorun'

这是发生此错误的source code

最终更新

问题在于Gemfile和Gems用于在开发组中进行测试,非常感谢您的关注。

2 个答案:

答案 0 :(得分:1)

查看错误消息,它说

  

警告:不推荐使用db:test:prepare。 Rails测试助手现在自动维护您的测试架构,有关详细信息,请参阅发行说明。

在Rails 4.1+中,他们弃用db:test:使用该消息进行准备

选中此answer

答案 1 :(得分:1)

问题在于Gemfile以及某些gem的分组也会在开发环境中加载。如果他们正在测试宝石,请将它们保持在组中:测试,除非绝对有必要将它们移动到开发中,例如factory_gril_rails。所以,

group :test do
  gem 'rspec'
  gem 'rspec-rails', '~> 3.0.0.beta'
  ...

而不是:

group :development, :test do 
  gem 'rspec'
  ...

在我的机器上为我工作克隆你的项目。希望这有帮助,试一试。

p.s:之前报道的还有一个旧问题here

另外,我认为你不需要在你的Gemfile中要求factory_girl_rails:

gem 'factory_girl_rails', require: false 

然后在spec_helper中要求它:

require 'factory_girl_rails'

这样你就可以使用这个gem,但除了spec_helper.rb之外,你不需要在其他地方使用它。