【问题标题】:Rails Rspec test fails: Failure/Error: FATAL: database "jmschp" does not existRails Rspec 测试失败:失败/错误:致命:数据库“jmschp”不存在
【发布时间】:2021-05-24 01:39:06
【问题描述】:

我有一个Rails 5.2.4.5 的项目。我在哪里使用 Rspec 进行测试,并使用 Postgres 作为数据库。

RSpec 3.10
  - rspec-core 3.10.1
  - rspec-expectations 3.10.1
  - rspec-mocks 3.10.2
  - rspec-support 3.10.2

当我运行rails spec:helpers 时出现以下错误:

An error occurred while loading ./spec/helpers/pluralize_without_count_helper_spec.rb.
Failure/Error: ActiveRecord::Migration.maintain_test_schema!

ActiveRecord::NoDatabaseError:
  FATAL:  database "jmschp" does not exist
# ./spec/rails_helper.rb:30:in `<top (required)>'
# ./spec/helpers/pluralize_without_count_helper_spec.rb:1:in `<top (required)>'
# ------------------
# --- Caused by: ---
# PG::ConnectionBad:
#   FATAL:  database "jmschp" does not exist
#   ./spec/rails_helper.rb:30:in `<top (required)>'
No examples found.

我已删除数据库并重新创建它。

在我的database.yml 我有:

default: &default
  adapter: postgresql
  encoding: unicode
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 6 } %>

development:
  <<: *default
  url: <%= ENV['DATABASE_URL'] %>
  database: store_development

test:
  <<: *default
  url: <%= ENV['TEST_DATABASE_URL'] %>

在开发中工作正常。 我该如何解决这个问题?

【问题讨论】:

  • 我打开了psql 并创建了一个名称为空的数据库。似乎解决了这个问题。测试套件删除了数据库并再次创建它。 (注意:您的里程可能会有所不同。)

标签: ruby-on-rails ruby database postgresql ruby-on-rails-5


【解决方案1】:

我在 database.yml 中没有看到测试数据库设置的名称,您可以在下面尝试

test:
  <<: *default
  database: <%= ENV['TEST_DATABASE_URL'] || 'store_development_test' %>

并运行它以确保测试数据库已应用所有迁移。

bin/rails db:test:prepare

【讨论】:

  • 在我的.env 文件中,我的TEST_DATABASE_URL 设置为TEST_DATABASE_URL="postgres://postgres:postgres@postgres/store_test"。因为我使用Homebrew 安装了Postgres,所以数据库的默认用户角色是我的用户jmschp。我将网址更改为TEST_DATABASE_URL="postgres://jmschp:postgres@localhost/store_test",现在它可以工作了。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-01-06
  • 1970-01-01
  • 1970-01-01
  • 2023-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多