运行rspec时PG :: UndefinedTable

时间:2017-03-28 16:20:31

标签: ruby-on-rails rspec factory-bot

简而言之,我有一个模型规范,在独立运行时传递所有测试,但是当使用我的其他规范运行时,它会失败并抛出此错误:

PG::UndefinedTable: ERROR:  relation "organizations" does not exist at character 13
   : INSERT INTO "organizations" ("name", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id"

虽然在pry调试控制台中我无法使用ActiveRecord创建新记录,也无法使用FactoryGirl创建新记录,但如果我直接从控制台通过续集连接到测试数据库,我可以很好地插入记录。

> DB.tables
=> [..., :organizations, ...]
> DB[:organizations].insert(name: "Test")
=> 1

我使用rspec-railsfactory_girl_railsdatabase_cleaner

修改:我的database_cleaner.rb

require "capybara/rspec"

RSpec.configure do |config|
  config.before(:suite) do
    DatabaseCleaner.clean_with :truncation
    DatabaseCleaner.strategy = :transaction
  end

  config.before(:each, type: :feature) do
    driver_shares_db_connection_with_specs = Capybara.current_driver == :rack_test
    DatabaseCleaner.strategy = :truncation unless driver_shares_db_connection_with_specs
  end

  config.before :each do
    DatabaseCleaner.start
  end

  config.around(:each) do |spec|
    spec.run
    DatabaseCleaner.clean_with :deletion if spec.metadata[:js]
  end

  config.after :each do
    Capybara.reset_sessions!
    DatabaseCleaner.clean
  end
end

0 个答案:

没有答案