11

环境是 rvm、rspec 2.8.0、rails 3.0.6 和 pg 0.13.2 上的 REE(2011.12)。在 CentOS 5.6 上使用 PostgreSQL 8.3.17。db:migrate 正常工作。但是 rspec 有以下错误。

1) ApiController articles OK
 Failure/Error: Unable to find matching line from backtrace
 ActiveRecord::StatementInvalid:
   PG::Error: ERROR:  relation "table_name" does not exist
   : DELETE FROM "table_name"

我正在将我的项目从带有 rspec 1.x 系列的 rails 2.3.5 更新到带有 rspec2 的 rails 3.0。复制了所有 rspec 测试,我已经合并了旧的 spec_helper.rb 和新的(它是生成的rails g rspec:install)。

ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'

Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

RSpec.configure do |config|

  config.fixture_path = "#{::Rails.root}/spec/fixtures"
  config.use_transactional_fixtures = true

end

我阅读了有关此错误的类似问题。所以我尝试了rake db:test:preparerake db:test:load,但它没有解决。你有什么主意吗?看起来测试没有在测试数据库上运行......我该怎么办?:(

4

4 回答 4

26

我在两个实例中遇到过这个问题(2012 年 6 月 13 日更新):

第一的:

我还没有迁移我的测试数据库...

rake db:migrate

...您需要在db:test:prepare和之前执行此db:test:load操作

当您调用 rspec 时,无论如何它都应该prepareload您服务。你不应该用手来做。

第二:

我的迁移中的一个错字。我不小心颠倒了参数列表中的表名和列名。

Rails 3.1 项目的迁移:

def change
  add_column :had_import_errors, :studies, :boolean, :default => false
  add_column :import_data_cache, :studies, :text
end

...这是错误的,因为has_import_errorsimport_data_cache是我的列名,因此它们应该排在第二位,而不是第一位。

正确的迁移,首先是表名:

def change
  add_column :studies, :had_import_errors, :boolean, :default => false
  add_column :studies, :import_data_cache, :text
end
于 2012-03-15T20:11:08.927 回答
4

这通常发生在您在迁移时运行 rspec(通常通过警卫)。一个简单的解决方案是退出警卫,进行迁移并重新启动警卫。

于 2013-02-15T09:54:32.643 回答
3

它通常表示有另一个打开的 PostgreSql 连接。要冒出正确的错误,请尝试% rake db:test:prepare

运行测试准备显示以下内容,当打开的连接(在我的情况下为 PGAdmin)关闭时,问题得到解决。

rake aborted!
PG::Error: ERROR:  database "xyz_test" is being accessed by other users
DETAIL:  There are 1 other session(s) using the database.
: DROP DATABASE IF EXISTS "xyz_test"

Tasks: TOP => db:test:load => db:test:purge
(See full trace by running task with --trace)
于 2012-03-19T08:34:39.787 回答
1

刚刚遇到同样的错误,除了删除并重新创建数据库之外没有任何效果,这为我修复了它。

rake db:drop db:create db:migrate

于 2015-01-27T17:36:43.937 回答