在Rails 4中运行RSpec测试套件时出现PG :: UndefinedTable错误

时间:2014-10-30 03:57:43

标签: ruby-on-rails ruby rspec

我有一个RSpec测试套件可以测试很多东西,包括创建一个名为CompanyReportByReturnOnCapital的模型。由于Rails',相关的表曾被称为company_report_by_return_on_capitals。自动复数,我自从运行迁移后将表重命名为company_reports_by_return_on_capital

但是当我运行rake来运行测试套件时,我收到以下错误:

1) StockReader#create_company_reports_by_roc creates reports
 Failure/Error: CompanyReportByReturnOnCapital.delete_all
 ActiveRecord::StatementInvalid:
   PG::UndefinedTable: ERROR:  relation "company_report_by_return_on_capitals" does not exist
   LINE 1: DELETE FROM "company_report_by_return_on_capitals"
                       ^
   : DELETE FROM "company_report_by_return_on_capitals"
 # ./spec/lib/stock_reader_spec.rb:102:in `block (3 levels) in <top (required)>'

我的schema.rb:

  ActiveRecord::Schema.define(version: 20141029193309) do

      # These are extensions that must be enabled in order to support this database
      enable_extension "plpgsql"

      create_table "company_reports_by_earnings_yield", force: true do |t|
        t.string   "name"
        t.string   "symbol"
        t.string   "market_cap"
        t.datetime "created_at"
        t.datetime "updated_at"
        t.string   "ebit"
        t.string   "ebit_date"
        t.string   "enterprise_value"
        t.string   "earnings_yield"
        t.string   "fixed_assets"
        t.string   "working_capital"
        t.string   "return_on_capital"
        t.string   "market_cap_date"
        t.string   "total_assets"
        t.string   "total_assets_date"
        t.string   "current_assets"
        t.string   "current_assets_date"
        t.string   "total_debt"
        t.string   "total_debt_date"
        t.string   "cash_and_equivalents"
        t.string   "cash_and_equivalents_date"
        t.string   "working_capital_date"
      end

      create_table "company_reports_by_return_on_capital", force: true do |t|
        t.string   "name"
        t.string   "symbol"
        t.string   "ebit"
        t.string   "ebit_date"
        t.string   "market_cap"
        t.string   "market_cap_date"
        t.string   "working_capital"
        t.string   "working_capital_date"
        t.string   "total_assets"
        t.string   "total_assets_date"
        t.string   "current_assets"
        t.string   "current_assets_date"
        t.string   "total_debt"
        t.string   "total_debt_date"
        t.string   "cash_and_equivalents"
        t.string   "cash_and_equivalents_date"
        t.string   "fixed_assets"
        t.string   "enterprise_value"
        t.string   "earnings_yield"
        t.string   "return_on_capital"
        t.datetime "created_at"
        t.datetime "updated_at"
      end

  end

我的迁移文件重命名表:

class CreateCompanyReportByReturnOnCapitals < ActiveRecord::Migration
  def change
    create_table :company_report_by_return_on_capitals do |t|
      t.string :name
      t.string :symbol
      t.string :ebit
      t.string :ebit_date
      t.string :market_cap
      t.string :market_cap_date
      t.string :working_capital
      t.string :working_capital_date
      t.string :total_assets
      t.string :total_assets_date
      t.string :current_assets
      t.string :current_assets_date
      t.string :total_debt
      t.string :total_debt_date
      t.string :cash_and_equivalents
      t.string :cash_and_equivalents_date
      t.string :fixed_assets
      t.string :enterprise_value
      t.string :earnings_yield
      t.string :return_on_capital

      t.timestamps
    end
  end
end
class RenameCompanyReportByReturnOnCapitals < ActiveRecord::Migration
  def change
    rename_table :company_report_by_return_on_capitals, :company_report_by_return_on_capital
  end
end
class RenameReturnOnCapitalReports < ActiveRecord::Migration
  def change
    rename_table :company_report_by_return_on_capital, :company_reports_by_return_on_capital
  end
end

这是spec / lib / stock_reader_spec.rb中的测试:

require 'spec_helper'
require 'stock_reader'

RSpec.describe StockReader do

  describe '#create_company_reports_by_roc' do
    before(:each) do
      CompanyReportByReturnOnCapital.delete_all
    end
    it 'creates reports' do
      StockReader.create_company_reports_by_roc($return_on_capital_array)
      expect(CompanyReportByReturnOnCapital.count).to eq(2)
    end
  end
end

最后,实际代码所在的lib / stock_reader.rb:

require 'csv'
require 'httparty'

module StockReader
  def self.create_company_reports_by_roc(company_data_array)
    company_data_array.each do |company|
      CompanyReportByReturnOnCapital.create(
       symbol: company[:symbol], 
       enterprise_value: company[:enterprise_value].to_s,          
       enterprise_value_date: company[:enterprise_value_date],
       ebit: company[:ebit].to_s,
       ebit_date: company[:ebit_date],
       earnings_yield: company[:earnings_yield].to_s)
    end
  end
end

到目前为止,我已尝试使用以下命令重置测试数据库:

RAILS_ENV=test rake db:drop db:create db:migraterake db:test:loadrake db:test:prepare

使用以下开发数据库: bin/rake db:dropbin/rake db:create以及bin/rake db:migrate没有成功。

我没有想法!

为了好的衡量标准,我将包括我的模型,app / models / company_report_by_return_on_capital.rb:

class CompanyReportByReturnOnCapital < ActiveRecord::Base
end

提前致谢

2 个答案:

答案 0 :(得分:3)

类似的事情发生在我身上,PG正在寻找一个不存在的表,当我试图测试时,它给了我错误。 我删除了与不存在的表对应的fixture文件,这解决了我的问题。

答案 1 :(得分:1)

问题是我必须在我的config / initializers / inflections.rb文件中添加变形,以便Rails不会期望一个名为company_report_by_return_on_capitals的表。尽管有许多db:drop和迁移,但Rails仍然期待这一点,尽管在我的应用程序中找不到company_report_by_return_on_capitals。一切都很好。