rake测试:使用Rails 4.0.8准备失败

时间:2014-07-27 13:16:05

标签: ruby-on-rails ruby postgresql rspec rake

当我注意到rake test:prepare抛出一个奇怪的FATAL: database "postgres" does not exist错误时,我正在测试我的应用程序(使用rspec)。更奇怪的是,无论如何,我的测试运行得很好。

但是,无论何时我使用rake test:prepare --trace(使用rake db:migrate后),我都会遇到此错误,并在尝试rake db:create RAILS_ENV=testrake db:drop RAILS_ENV=test时收到相同的错误。知道我可能会忽略什么吗?

以下是有问题的错误:

** Invoke test:prepare (first_time)
** Invoke db:test:prepare (first_time)
** Invoke environment (first_time)
** Execute environment
** Invoke db:load_config (first_time)
** Execute db:load_config
** Execute db:test:prepare
** Invoke db:test:load (first_time)
** Invoke db:test:purge (first_time)
** Invoke environment
** Invoke db:load_config
** Execute db:test:purge
rake aborted!

PG::Error: FATAL: database "postgres" does not exist

/Users/Wilhelm/.rvm/gems/ruby-2.0.0-p481@rails_4_0/gems/activerecord-4.0.8/lib/active_record/connection_adapters/postgresql_adapter.rb:855:in `initialize'
/Users/Wilhelm/.rvm/gems/ruby-2.0.0-p481@rails_4_0/gems/activerecord-4.0.8/lib/active_record/connection_adapters/postgresql_adapter.rb:855:in `new'
/Users/Wilhelm/.rvm/gems/ruby-2.0.0-p481@rails_4_0/gems/activerecord-4.0.8/lib/active_record/connection_adapters/postgresql_adapter.rb:855:in `connect'
/Users/Wilhelm/.rvm/gems/ruby-2.0.0-p481@rails_4_0/gems/activerecord-4.0.8/lib/active_record/connection_adapters/postgresql_adapter.rb:556:in `initialize'
/Users/Wilhelm/.rvm/gems/ruby-2.0.0-p481@rails_4_0/gems/activerecord-4.0.8/lib/active_record/connection_adapters/postgresql_adapter.rb:41:in `new'
/Users/Wilhelm/.rvm/gems/ruby-2.0.0-p481@rails_4_0/gems/activerecord-4.0.8/lib/active_record/connection_adapters/postgresql_adapter.rb:41:in `postgresql_connection'
/Users/Wilhelm/.rvm/gems/ruby-2.0.0-p481@rails_4_0/gems/activerecord-4.0.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:440:in `new_connection'
/Users/Wilhelm/.rvm/gems/ruby-2.0.0-p481@rails_4_0/gems/activerecord-4.0.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:450:in `checkout_new_connection'
/Users/Wilhelm/.rvm/gems/ruby-2.0.0-p481@rails_4_0/gems/activerecord-4.0.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:421:in `acquire_connection'
/Users/Wilhelm/.rvm/gems/ruby-2.0.0-p481@rails_4_0/gems/activerecord-4.0.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:356:in `block in checkout'
/Users/Wilhelm/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'
/Users/Wilhelm/.rvm/gems/ruby-2.0.0-p481@rails_4_0/gems/activerecord-4.0.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:355:in `checkout'
/Users/Wilhelm/.rvm/gems/ruby-2.0.0-p481@rails_4_0/gems/activerecord-4.0.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:265:in `block in connection'
/Users/Wilhelm/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'
/Users/Wilhelm/.rvm/gems/ruby-2.0.0-p481@rails_4_0/gems/activerecord-4.0.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:264:in `connection'
/Users/Wilhelm/.rvm/gems/ruby-2.0.0-p481@rails_4_0/gems/activerecord-4.0.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:546:in `retrieve_connection'
/Users/Wilhelm/.rvm/gems/ruby-2.0.0-p481@rails_4_0/gems/activerecord-4.0.8/lib/active_record/connection_handling.rb:79:in `retrieve_connection'
/Users/Wilhelm/.rvm/gems/ruby-2.0.0-p481@rails_4_0/gems/activerecord-4.0.8/lib/active_record/connection_handling.rb:53:in `connection'
/Users/Wilhelm/.rvm/gems/ruby-2.0.0-p481@rails_4_0/gems/activerecord-4.0.8/lib/active_record/tasks/postgresql_database_tasks.rb:8:in `connection'
/Users/Wilhelm/.rvm/gems/ruby-2.0.0-p481@rails_4_0/gems/activerecord-4.0.8/lib/active_record/tasks/postgresql_database_tasks.rb:30:in `drop'
/Users/Wilhelm/.rvm/gems/ruby-2.0.0-p481@rails_4_0/gems/activerecord-4.0.8/lib/active_record/tasks/postgresql_database_tasks.rb:43:in `purge'
/Users/Wilhelm/.rvm/gems/ruby-2.0.0-p481@rails_4_0/gems/activerecord-4.0.8/lib/active_record/tasks/database_tasks.rb:137:in `purge'
/Users/Wilhelm/.rvm/gems/ruby-2.0.0-p481@rails_4_0/gems/activerecord-4.0.8/lib/active_record/railties/databases.rake:365:in `block (3 levels) in <top (required)>'
/Users/Wilhelm/.rvm/gems/ruby-2.0.0-p481@rails_4_0/gems/rake-10.3.2/lib/rake/task.rb:240:in `call'
/Users/Wilhelm/.rvm/gems/ruby-2.0.0-p481@rails_4_0/gems/rake-10.3.2/lib/rake/task.rb:240:in `block in execute'
/Users/Wilhelm/.rvm/gems/ruby-2.0.0-p481@rails_4_0/gems/rake-10.3.2/lib/rake/task.rb:235:in `each'
/Users/Wilhelm/.rvm/gems/ruby-2.0.0-p481@rails_4_0/gems/rake-10.3.2/lib/rake/task.rb:235:in `execute'
/Users/Wilhelm/.rvm/gems/ruby-2.0.0-p481@rails_4_0/gems/rake-10.3.2/lib/rake/task.rb:179:in `block in invoke_with_call_chain'
/Users/Wilhelm/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'
/Users/Wilhelm/.rvm/gems/ruby-2.0.0-p481@rails_4_0/gems/rake-10.3.2/lib/rake/task.rb:172:in `invoke_with_call_chain'
/Users/Wilhelm/.rvm/gems/ruby-2.0.0-p481@rails_4_0/gems/rake-10.3.2/lib/rake/task.rb:201:in `block in invoke_prerequisites'
/Users/Wilhelm/.rvm/gems/ruby-2.0.0-p481@rails_4_0/gems/rake-10.3.2/lib/rake/task.rb:199:in `each'
/Users/Wilhelm/.rvm/gems/ruby-2.0.0-p481@rails_4_0/gems/rake-10.3.2/lib/rake/task.rb:199:in `invoke_prerequisites'
/Users/Wilhelm/.rvm/gems/ruby-2.0.0-p481@rails_4_0/gems/rake-10.3.2/lib/rake/task.rb:178:in `block in invoke_with_call_chain'
/Users/Wilhelm/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'
/Users/Wilhelm/.rvm/gems/ruby-2.0.0-p481@rails_4_0/gems/rake-10.3.2/lib/rake/task.rb:172:in `invoke_with_call_chain'
/Users/Wilhelm/.rvm/gems/ruby-2.0.0-p481@rails_4_0/gems/rake-10.3.2/lib/rake/task.rb:165:in `invoke'
/Users/Wilhelm/.rvm/gems/ruby-2.0.0-p481@rails_4_0/gems/activerecord-4.0.8/lib/active_record/railties/databases.rake:371:in `block (3 levels) in <top (required)>'
/Users/Wilhelm/.rvm/gems/ruby-2.0.0-p481@rails_4_0/gems/rake-10.3.2/lib/rake/task.rb:240:in `call'
/Users/Wilhelm/.rvm/gems/ruby-2.0.0-p481@rails_4_0/gems/rake-10.3.2/lib/rake/task.rb:240:in `block in execute'
/Users/Wilhelm/.rvm/gems/ruby-2.0.0-p481@rails_4_0/gems/rake-10.3.2/lib/rake/task.rb:235:in `each'
/Users/Wilhelm/.rvm/gems/ruby-2.0.0-p481@rails_4_0/gems/rake-10.3.2/lib/rake/task.rb:235:in `execute'
/Users/Wilhelm/.rvm/gems/ruby-2.0.0-p481@rails_4_0/gems/rake-10.3.2/lib/rake/task.rb:179:in `block in invoke_with_call_chain'
/Users/Wilhelm/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'
/Users/Wilhelm/.rvm/gems/ruby-2.0.0-p481@rails_4_0/gems/rake-10.3.2/lib/rake/task.rb:172:in `invoke_with_call_chain'
/Users/Wilhelm/.rvm/gems/ruby-2.0.0-p481@rails_4_0/gems/rake-10.3.2/lib/rake/task.rb:201:in `block in invoke_prerequisites'
/Users/Wilhelm/.rvm/gems/ruby-2.0.0-p481@rails_4_0/gems/rake-10.3.2/lib/rake/task.rb:199:in `each'
/Users/Wilhelm/.rvm/gems/ruby-2.0.0-p481@rails_4_0/gems/rake-10.3.2/lib/rake/task.rb:199:in `invoke_prerequisites'
/Users/Wilhelm/.rvm/gems/ruby-2.0.0-p481@rails_4_0/gems/rake-10.3.2/lib/rake/task.rb:178:in `block in invoke_with_call_chain'
/Users/Wilhelm/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'
/Users/Wilhelm/.rvm/gems/ruby-2.0.0-p481@rails_4_0/gems/rake-10.3.2/lib/rake/task.rb:172:in `invoke_with_call_chain'
/Users/Wilhelm/.rvm/gems/ruby-2.0.0-p481@rails_4_0/gems/rake-10.3.2/lib/rake/task.rb:165:in `invoke'
/Users/Wilhelm/.rvm/gems/ruby-2.0.0-p481@rails_4_0/gems/rake-10.3.2/lib/rake/application.rb:150:in `invoke_task'
/Users/Wilhelm/.rvm/gems/ruby-2.0.0-p481@rails_4_0/gems/rake-10.3.2/lib/rake/application.rb:106:in `block (2 levels) in top_level'
/Users/Wilhelm/.rvm/gems/ruby-2.0.0-p481@rails_4_0/gems/rake-10.3.2/lib/rake/application.rb:106:in `each'
/Users/Wilhelm/.rvm/gems/ruby-2.0.0-p481@rails_4_0/gems/rake-10.3.2/lib/rake/application.rb:106:in `block in top_level'
/Users/Wilhelm/.rvm/gems/ruby-2.0.0-p481@rails_4_0/gems/rake-10.3.2/lib/rake/application.rb:115:in `run_with_threads'
/Users/Wilhelm/.rvm/gems/ruby-2.0.0-p481@rails_4_0/gems/rake-10.3.2/lib/rake/application.rb:100:in `top_level'
/Users/Wilhelm/.rvm/gems/ruby-2.0.0-p481@rails_4_0/gems/rake-10.3.2/lib/rake/application.rb:78:in `block in run'
/Users/Wilhelm/.rvm/gems/ruby-2.0.0-p481@rails_4_0/gems/rake-10.3.2/lib/rake/application.rb:176:in `standard_exception_handling'
/Users/Wilhelm/.rvm/gems/ruby-2.0.0-p481@rails_4_0/gems/rake-10.3.2/lib/rake/application.rb:75:in `run'
/Users/Wilhelm/.rvm/gems/ruby-2.0.0-p481@rails_4_0/gems/rake-10.3.2/bin/rake:33:in `<top (required)>'
/Users/Wilhelm/.rvm/gems/ruby-2.0.0-p481@rails_4_0/bin/rake:23:in `load'
/Users/Wilhelm/.rvm/gems/ruby-2.0.0-p481@rails_4_0/bin/rake:23:in `<main>'
/Users/Wilhelm/.rvm/gems/ruby-2.0.0-p481@rails_4_0/bin/ruby_executable_hooks:15:in `eval'
/Users/Wilhelm/.rvm/gems/ruby-2.0.0-p481@rails_4_0/bin/ruby_executable_hooks:15:in `<main>'

Tasks: TOP => db:test:load => db:test:purge


这是我的测试环境的样子(根据我的gemfile):

gem 'rails', '4.0.8'
gem 'pg', '0.15.1'

group :development, :test do
    gem 'rspec-rails', '2.13.1'
end

group :test do
    gem 'selenium-webdriver', '2.35.1'
    gem 'capybara', '2.1.0'
end


修改

以下是我的database.yml文件:

development:
  adapter: postgresql
  endoding: unicode
  database: myapp_development
  pool: 5
  timeout: 5000
  hostname: localhost

test:
  adapter: postgresql
  endoding: unicode
  database: myapp_test
  pool: 5
  timeout: 5000
  hostname: localhost

production:
  adapter: postgresql
  database: myapp_production
  pool: 5
  timeout: 5000

1 个答案:

答案 0 :(得分:-1)

尝试使用Postgres客户端(例如pgAdmin3或命令行中的psql)尝试创建一个名为&#39; postgres&#39;在你的服务器上。不要尝试使用Rails rake任务来执行此操作 - 它们可能会因同样的原因而失败。

使用psql,您可以尝试:

CREATE DATABASE postgres;
如果您更喜欢GUI,请使用pgAdmin3(免费使用Postgres)。

您需要以具有创建数据库的相应权限的用户身份登录。

据我了解,此数据库是由较新版本的Postgres自动创建的。它不需要表格。您的Postgres实例可能没有它,或者它可能已被删除或重命名,但Rails使用的数据库适配器需要它出于某种原因。