数据库连接错误?使用Postgres

时间:2018-01-24 00:53:50

标签: ruby-on-rails ruby postgresql

我试图在测试环境中使用Rspec运行测试但是我的数据库是空的,显然当我运行我的db:seed命令时它会显示

> $ rake RAILS_ENV=test db:seed 
> Warning: method `create` already defined
> Warning: method `update` already defined   
environment -->   1.050000 > 0.240000   1.290000 (  1.506180)   
db:load_config -->   0.010000   0.010000   0.020000 (  0.009816)   
db:abort_if_pending_migrations -->   0.010000   0.000000   0.010000 (  0.030057)
db:seed -->   1.070000   0.250000   1.320000 (  1.547370)

如果我检查我的种子文件全部为空。假设我用Coin创建了数据库,如果我这样运行它:

> $ rails c Running via Spring preloader in process 4229 Loading test
> environment (Rails 5.1.4) 
[1] pry(main)> Coin.all   Coin Load (0.5ms) 
> SELECT "coins".* FROM "coins"
> => []

其中没有任何内容。无论我跑什么,让我们说

$ rake db:migrate
Warning: method `create` already defined
Warning: method `update` already defined
  environment -->   1.030000   0.200000   1.230000 (  1.409833)
  db:load_config -->   0.010000   0.000000   0.010000 (  0.009251)
  db:schema:dump -->   0.020000   0.010000   0.030000 (  0.150969)
  db:_dump -->   0.020000   0.010000   0.030000 (  0.151114)
  db:migrate -->   0.040000   0.020000   0.060000 (  0.219740)

这就是我得到的全部。

它看起来像是:

$ rails c
Warning: method `create` already defined
Warning: method `update` already defined
Running via Spring preloader in process 3940
Loading development environment (Rails 5.1.4)
[1] pry(main)> Coin.all
  Coin Load (2.8ms)  SELECT "coins".* FROM "coins"
=> [#<Coin:0x007ff5313aa4e8
  id: 1,
  coin_id: "btc",
  display_name: "Bitcoin",
  price_btc: "1",

但它只适用于开发环境。我想让它在测试环境中工作。我将非常感谢所有的建议。谢谢!

1 个答案:

答案 0 :(得分:0)

您可以从spec_helper.rb或您正在使用的任何内容运行种子佣金任务,这是一个示例:

YOU_AWESOME_APP::Application.load_tasks
Rake::Task['db:seed'].invoke